|
|
|
| ||||||||||||||||||||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
Problem in sockets
hi i am trying to buid a server socket and a client socket which would communicate with each other,.. i build the following code but its i am getting no reponse from either side.. The only output i got is shown at the end..There is no error reported from the compiler either .. please can anyone figure out how to correct it?
CLIENTSOCKET: import java.io.; import java.net.; public class client3 { public static void main(String args[]) throws IOException,UnknownHostException { Socket client_socket = new Socket("localhost",9882); int rp = client_socket.getPort(); System.out.println("Client connected to server at port:"+rp); PrintWriter out = new PrintWriter(client_socket.getOutputStream()); BufferedReader in = new BufferedReader(new InputStreamReader(client_socket.getInputStream())); out.println("Hi,i am client connected to you server"); String str1 = new String(); String str1 = in.readLine(); //out.println(str1); System.out.println(str1); in.close(); out.close(); client_socket.close(); } } OUTPUT: Client connected to server at port:9882 SERVER SOCKET: import java.net.; import java.io.; public class server3 { public static void main(String args[]) throws IOException { Socket client_socket = null; ServerSocket server_socket = new ServerSocket(9882); System.out.println("Server ready,waiting for client: "); client_socket = server_socket.accept(); //int t=client_socket.getSoTimeout(); //System.out.print(t); PrintWriter out = new PrintWriter(client_socket.getOutputStream()); BufferedReader in = new BufferedReader(new InputStreamReader(client_socket.getInputStream())); out.println("Hi,this is Server Speaking"); String c = in.readLine(); String str = new String(); while(c!=null) { str=in.readLine(); } out.println("hi! this is Server , I received the followng message from you:"); out.println(str); System.out.println(str); out.close(); in.close(); client_socket.close(); server_socket.close(); } } OUTPUT: Server ready,waiting for client: ![]() |
|
#2
|
|||
|
|||
|
I ran into this recently... you might need to flush the inputwriter first in order to actually send your text
|
![]() |
| Viewing: Dev Shed Forums > Programming Languages > Java Help > Problem in sockets |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|
|