mga boss. i'm practicing socket programming in java. chat ni cya
the problem is, di maka.connect ang client if unahon ug run ang server para mu.create ug socket.
mu.connect ra ang client if naka.run na cya daan, then paabot cya mu.run ang server para mucreate ug ServerSocket, dha pa cya mu.connect. LAINA UI! hahaha. bali man nuon. dapat mag.una ang server then musunod ug connect ang client.
here is the code of the client connection.
kung kbw mo, post mo mga bossing. then add ug mga comments. tnx
Code:
try {
boolean connected = false;
do {
try {
connection = new Socket( "127.0.0.1", 1234 );
connected = true;
input_field.setEditable( true );
textArea.append( String.format( "Now connected to %s.\n",
connection.getInetAddress() ) );
} catch ( Exception e ) {
connected = false;
}
} while( !connected );
//read inputs
Scanner in = new Scanner( new InputStreamReader( connection.getInputStream() ) );
String line;
while ( ( line = in.nextLine() ) != null ) {
textArea.append( line + "\n" );
}
} catch ( IOException e ) {
textArea.append( String.format( "%s\n", e.getMessage() ) );
}