How to connect 2 different computers on a network using a chat application in Java? -
How to connect 2 different computers on a network using a chat application in Java? -
i have simple pair of client , server programs. client connects server , when connect, server replies "hello there" message. how should modify programme if want client , server programs run on different systems?
here code client side..
package practice; import java.io.*; import java.net.*; public class dailyadviceclient { public static void main(string args[]) { dailyadviceclient dac = new dailyadviceclient(); dac.go(); } public void go() { seek { socket incoming = new socket("127.0.0.1",4242); inputstreamreader stream = new inputstreamreader(incoming.getinputstream()); bufferedreader reader = new bufferedreader(stream); string advice = reader.readline(); reader.close(); system.out.println("today's advice "+advice); } catch(exception e) { system.out.println("client side error"); } } }
and here code server
package practice; import java.io.*; import java.net.*; public class dailyadviceserver { public static void main(string args[]) { dailyadviceserver das = new dailyadviceserver(); das.go(); } public void go() { seek { serversocket serversock = new serversocket(4242); while(true) { socket outgoing = serversock.accept(); printwriter author = new printwriter(outgoing.getoutputstream()); writer.println("hello there"); writer.close(); } } catch(exception e) { system.out.println("server side problem"); } } }
just alter "127.0.0.1" on client server's ip , create sure port 4242 open.
java
Comments
Post a Comment