• Post Reply Bookmark Topic Watch Topic
  • New Topic

Simple Socket: From Server to Client

 
Pullox Troy
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,
i have wrote two simple client and server class
<include bellow>
when i send a number(n) to the server,
i want the server print the 2*n and then return
(print) 2*n to the client.
but my code did not work properly.
why the client can't read from server ??
it just wait the number from server.
can any body tell me to do so ??
regars,
pullox
-------------------------------------------------
// SERVER CLASS
import java.io.*;
import java.net.*;
public class SquareServer {
public static void main(String[] args) throws IOException {
ServerSocket sSocket = null;
try {
sSocket = new ServerSocket(9999);
} catch(IOException ioe) {
System.out.println("Error");
System.exit(1);
}
Socket cSocket = null;
PrintWriter writerToSocket = null;
BufferedReader readerFromSocket = null;
try {
cSocket = sSocket.accept();
} catch(IOException ioe) {
System.out.println("Error");
System.exit(1);
}
writerToSocket = new PrintWriter(cSocket.getOutputStream());
readerFromSocket = new BufferedReader(new InputStreamReader(cSocket.getInputStream()));

String input = "";
while((input = readerFromSocket.readLine()) != null) {
long num = Long.valueOf(input).longValue();
num = 2*num;
System.out.println(num);
writerToSocket.println(num);
}
writerToSocket.close();
readerFromSocket.close();
cSocket.close();
sSocket.close();
}
}
// CLIENT CLASS
import java.io.*;
import java.net.*;
public class SquareClient {
public static void main(String[] args) throws IOException {
// initialize socket
Socket scSocket = null;
// initialize i/o stream
PrintWriter writerToSocket = null;
BufferedReader readerFromSocket = null;
// open socket and i/o stream
try {
scSocket = new Socket("localhost", 9999);
writerToSocket = new PrintWriter(scSocket.getOutputStream(), true);
readerFromSocket = new BufferedReader(new InputStreamReader(scSocket.getInputStream()));
} catch(IOException ie) {
System.out.println("Error::" + ie.toString());
}
BufferedReader readerFromStandard = new BufferedReader(new InputStreamReader(System.in));
String clientInput = "";
String serverResponse = "";
while((clientInput = readerFromStandard.readLine()) != null) {
// send data to server
writerToSocket.println(clientInput);
// receive result from server
serverResponse = readerFromSocket.readLine();
if(serverResponse != null) {
System.out.println(serverResponse);
}
}
writerToSocket.close();
readerFromSocket.close();
readerFromStandard.close();
scSocket.close();
}
}
 
Carl Trusiak
Sheriff
Posts: 3341
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your server is hung in the loop

Because your client is never sending null. When ever you create a straight socket and want the server to end, you have to ensure the Server and Clinet agree on how to end a communication. You can send a null to the Server by the line
writerToSocket.write(0);
However this creates another problem in your client. See if you can find it and find out how you can fix it
Hope this helps
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!