I created a simple socket server and a socket client
The server receive request from server and send same request back to the client.
Please see example code as below:
// socket server
package socket;
import java.net.*;
import java.io.*;
public class SimpleServer {
public SimpleServer(int listen_port) {
this.listen_port = listen_port;
}
public static void main(
String[] args) {
SimpleServer server = new SimpleServer(4444);
server.acceptConnections();
}
public void acceptConnections() {
try {
ServerSocket server = new ServerSocket(listen_port);
Socket incomingConnection = null;
while (true) {
incomingConnection = server.accept();
handleConnection(incomingConnection);
}
} catch (BindException e) {
System.out.println("Unable to bind to port " + listen_port);
} catch (IOException e) {
System.out.println("Unable to instantiate a ServerSocket on port: " + listen_port);
}
}
public void handleConnection(Socket incomingConnection) {
try {
InputStream inputFromSocket = incomingConnection.getInputStream();
BufferedReader streamReader = new BufferedReader(new InputStreamReader(inputFromSocket));
PrintWriter streamWriter = new PrintWriter(incomingConnection.getOutputStream());
StringBuffer sb = new StringBuffer();
String line = null;
int i = 0;
while((line=streamReader.readLine())!=null){
streamWriter.println(line);
break;
}
streamWriter.close();
streamReader.close();
incomingConnection.close();
} catch (Exception e) {
}
}
protected int listen_port;
}
//socket client
package socket;
import java.io.*;
import java.net.*;
import java.util.Iterator;
import java.util.List;
import java.math.*;
public class Simple {
public Simple(String host_ip, int host_port) {
this.host_ip = host_ip;
this.host_port = host_port;
}
public static void main(String[] args) {
Simple simple = new Simple("192.168.254.134", 4444);
simple.setUpConnection();
try {
String result = simple.getResponse("This is first request");
String result2 = simple.getResponse("This is second request");
simple.tearDownConnection();
}
catch (Exception e) {
System.out.println("error");
e.printStackTrace();
}
}
public void setUpConnection() {
try {
// Create new socket object
Socket client = new Socket(host_ip, host_port);
socketReader = new BufferedReader(new InputStreamReader(client.
getInputStream()));
socketWriter = new PrintWriter(client.getOutputStream());
} catch (UnknownHostException e) {
System.out.println(
"Error setting up socket connection: unknown host at " +
host_ip + ":" + host_port);
}
catch (IOException e) {
System.out.println("Error setting up socket connection: " + e);
}
}
public String getResponse(String request) {
StringBuffer fileLines = new StringBuffer();
try {
socketWriter.println(request);
socketWriter.flush();
String line = null;
int i = 0;
int chars = 0;
int sizeOfBuffer = 100;
char[] cbuf = new char[sizeOfBuffer];
while ( (chars = socketReader.read(cbuf)) >= 0) {
fileLines.append(cbuf, 0, chars);
}
} catch (Exception e) {
e.printStackTrace();
}
return fileLines.toString();
}
public void tearDownConnection() {
try {
socketWriter.close();
socketReader.close();
} catch (IOException e) {
System.out.println("Error tearing down socket connection: " + e);
}
}
protected BufferedReader socketReader;
protected PrintWriter socketWriter;
protected String host_ip;
protected int host_port;
}
I send two request at the same time.
Please see code in main method of Simple class
String result = simple.getResponse("first");
String result2 = simple.getResponse("second");
What I think is the socket server will send the same request back to the client.
So value of result and result2 should be 'first' and 'second'
But it always return empty String for result2.
What's the problem?
Thanks for your help
