I am developing a simple client server program using this link. My server is a Mac machine and client is a Windows machine (Win 10).
The client-server communication works fine. When I examined the bytes sent using Wireshark, in the first TCP packet only one character gets sent. And there is another TCP packet where the rest of the data gets sent.
i.e. If I send "qwerty", client send "q", server responds, then client sends "werty" . Similarly Server sends "Q", client responds then server sends "WERTY".
Here is my client server code. I am flushing the data after doing writeBytes().
How can I enforce the data to be sent in a single TCP packet?
Client Code:
import java.io.*;
import java.net.*;
class Client
{
public static void main(String argv[]) throws Exception
{
String sentence;
String modifiedSentence;
BufferedReader inFromUser = new BufferedReader( new InputStreamReader(System.in));
Socket clientSocket = new Socket("192.1.162.65", 6789);
DataOutputStream outToServer = new DataOutputStream(clientSocket.getOutputStream());
BufferedReader inFromServer = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
System.out.println("Enter some text: " + inFromServer);
sentence = inFromUser.readLine();
outToServer.writeBytes(sentence + '\n');
outToServer.flush();
modifiedSentence = inFromServer.readLine();
System.out.println("FROM SERVER: " + modifiedSentence);
clientSocket.close();
}
}
Server Code:
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.InputStreamReader;
import java.net.ServerSocket;
import java.net.Socket;
public class Server {
public static void createListener(ServerSocket serverSocket) throws Exception {
String clientSentence;
String capitalizedSentence;
@SuppressWarnings("resource")
ServerSocket welcomeSocket = new ServerSocket(6789);
while (true) {
@SuppressWarnings("resource")
Socket connectionSocket = welcomeSocket.accept();
BufferedReader inFromClient = new BufferedReader(new InputStreamReader(connectionSocket.getInputStream()));
DataOutputStream outToClient = new DataOutputStream(connectionSocket.getOutputStream());
clientSentence = inFromClient.readLine();
System.out.println("Received: " + clientSentence);
capitalizedSentence = clientSentence.toUpperCase() + '\n';
outToClient.writeBytes(capitalizedSentence);
outToClient.flush();
}
}
public static void main(String[] args) throws Exception {
Server.createListener(null);
}
}
Edit 1: The reason behind this question is, I am trying to use the same server code to send data to another client that is not in my control. In that case, the above code sends just the first TCP packet. I do not see the second TCP packet on Wireshark. Any suggestion on how I can go about debugging it?