blob: dbaa39167c42b323302014d9bd1f334549b9132f (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
|
package backend;
import java.net.* ;
import javax.swing.JFrame ;
import java.io.* ;
public class ChatClientCMD
{
/**
*
*
* @param args
* @throws IOException
*/
public static void main( String[] args ) throws IOException
{
// Get server IP
BufferedReader keyboard = new BufferedReader( new InputStreamReader( System.in ) ) ;
System.out.print( "\nPlease enter the IP of the desired server: " ); // GUI: IP BOX
String serverIP = keyboard.readLine() ;
// Open your connection to a server, at port 6789
Socket socket = new Socket( serverIP , 6789 ) ;
// Create communication streams
DataInputStream dataStreamIn = new DataInputStream( socket.getInputStream() ) ;
DataOutputStream dataStreamOut = new DataOutputStream( socket.getOutputStream() ) ;
// Establish connection, make user-name
System.out.print( "\nConnection established,\nPlease enter a username: " ) ;
String username = keyboard.readLine() ;
dataStreamOut.writeUTF( username ) ;
/////////////////////////
//// Program Running ////
/////////////////////////
// Thread for sending messages
Thread sendMessage = new Thread(new Runnable()
{
@Override
public void run()
{
System.out.print( "\n-= Type /help for a list of commands =-" ) ;
while( true )
{
try
{
String messageOut = keyboard.readLine() ;
dataStreamOut.writeUTF( messageOut ) ;
}
catch( IOException x )
{
x.printStackTrace() ;
System.exit( 0 ) ;
}
}
}
}) ;
// Thread for receiving messages
Thread receiveMessage = new Thread( new Runnable()
{
@Override
public void run()
{
while( true )
{
try
{
String messageIn = dataStreamIn.readUTF() ;
System.out.printf( "%n%s\n", messageIn ) ;
}
catch( IOException x )
{
x.printStackTrace() ;
System.exit( 0 ) ;
}
}
}
});
// Begin threads
sendMessage.start() ;
receiveMessage.start() ;
}
}
// end class ChatClient
|