Here is a simple implementation of TCPServer where all i wanted to do is send a string to client when requested.
import java.util.*;
import java.io.*;
import java.net.*;
class TCPServer{
public static void main(String args[]) throws Exception{
ServerSocket server = new ServerSocket(4888);
while(true){
Socket client = server.accept();
DataOutputStream out = new DataOutputStream(client.getOutputStream());
String send = "Bhushan Patil \n 11-237 \n CMPN";
out.writeBytes(send);
}
}
}
But on clinet side only Bhushan Patil is shown not the rest of the string.
Here is the code of client.
import java.util.*;
import java.io.*;
import java.net.*;
class TCPClient{
public static void main(String args[]) throws Exception{
Socket client = new Socket("localhost",4888);
BufferedReader in = new BufferedReader(new InputStreamReader(client.getInputStream ()));
String display = in.readLine();
System.out.println(display);
}
}
Can anyone explain why it is happening? When i do
System.out.println(send);
I get the whole string with \n so i am assuming that you dont get new lines. Correct me if i am wrong. Thanx
Update the client code as below :
String display = null
while ((display = in.readLine()) != null)
{
System.out.println(display );
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With