hi all,
i am trying o study socket programming in java and trying to create a simple chat program, i found this code on a tutorial on web and tried playing with it, the problem is it seems it cant connect to the server.
this is my code for the server side:
import java.io.*; import java.net.*; public class ServerSide{ public static void main(String[] args)throws Exception { BufferedReader In = new BufferedReader(new InputStreamReader(System.in)); String data = In.readLine(); ServerSocket srvr = new ServerSocket(8000); Socket skt = srvr.accept(); System.out.print("Server has connected!\n"); PrintWriter out = new PrintWriter(skt.getOutputStream(), true); System.out.print("Sending string: '" + data + "'\n"); out.print(data); out.close(); skt.close(); srvr.close(); } }
and this is my code for the client side:
import java.io.*; import java.net.*; public class ClientSide{ public static void main(String[] args)throws Exception { Socket skt = new Socket("192.168.5.236", 8000); BufferedReader in = new BufferedReader(new InputStreamReader(skt.getInputStream())); System.out.print("Received string: '"); while (!in.ready()) {} System.out.println(in.readLine()); // Read one line and output it System.out.print("'\n"); in.close(); } }
this is the error when i try to run the client side.
D:\Sockets Test>java ClientSide
Exception in thread "main" java.net.ConnectException: Connection refused: connec
t
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.PlainSocketImpl.doConnect(Unknown Source)
at java.net.PlainSocketImpl.connectToAddress(Unknown Source)
at java.net.PlainSocketImpl.connect(Unknown Source)
at java.net.SocksSocketImpl.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at java.net.Socket.<init>(Unknown Source)
at java.net.Socket.<init>(Unknown Source)
at ClientSide.main(ClientSide.java:8)
anyone can help me where i am wrong on this?. thanks.