Why am i getting weird character in socket inputstream - java

// Portion of code copied from the senders side
clientSocket = new Socket(successor.IP, successor.PORT);
toServer = new BufferedWriter(new OutputStreamWriter(clientSocket.getOutputStream()));
String message = "deleteme\n"+predecessor.IP+","+predecessor.PORT;
toServer.write(message);
toServer.newLine();
toServer.flush();
// Portion of code copied from the receivers side
fromClient = new BufferedReader(new InputStreamReader(socket.getInputStream()));
toClient = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
input = fromClient.readLine();
On the receivers side there are weird characters before the deleteme. Like the clubs ♣.
I want to know from where do these characters come and how to fix the problem? The temporary fix that I am doing is that before I send the deleteme message I send some garbage data like abcd. Then the deleteme get there as it is.

Related

Java socket: Can't send proper data

I have a Java TCP server, and an android TCP client. I'm trying to send data from client to server. Sending the data seems to be working fine, but the data that is sent, seems corrupted.
Socket connectionSocket = socket.accept();
BufferedReader inFromClient = new BufferedReader(new InputStreamReader( connectionSocket.getInputStream()));
DataOutputStream outToClient = new DataOutputStream(connectionSocket.getOutputStream());
clientSentence = inFromClient.readLine();
System.out.println(clientSentence);
System.out.println(clientSentence.split(":")[0]);
if(clientSentence.split(":")[0].equals("packet"))
{
When the server receives the data, the prints show something like this in the console:
packet:user:pass
packet
Which is as expected. But still my if isn't returning true. As if the "packet" string got from socket, is different from the one I type with keyboard in my source. I can't even copy the text from console. When I copy with mouse and paste it somewhere, it only copies the first character.
I use the same structures on client side and send the packet with [DataOutputStream].writeChars(message)
I don't know if it's a different coding of characters that cause this, or something else. Also it's worth noting that when i capture the text with wireshark, the string is something like ".p.a.c.k.e.t"
Thanks.
EDIT: As asked, client side code is something like this:
Socket clientSocket = new socket("127.0.0.1", 1234);
BufferedReader inFromServer = new BufferedReader(new InputStreamReader(clientSocket.getInputStream());
DataOutputStream outToServer = new DataOutputStream(clientSocket.getOutputStream());
String message = "packet:" + username + ":" + password + "\n";
outToServer.writeChars(message);
It's on an android device.

Trouble sending/receiving strings for client/server

I am having a little bit of trouble sending and receiving strings from client to server. Assume I have the sockets set up correctly.
This is what I am using to send/receive server side:
fromClient = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
toClient = new BufferedWriter(new OutputStreamWriter(clientSocket.getOutputStream()));
String clientInput;
clientInput = fromClient.readLine();
is how my server receives inputs from the client.
Client side same deal:
toServer = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
fromServer = new BufferedReader(new InputStreamReader(socket.getInputStream()));
inputLine = bufferedReader.readLine(); //inputLine reads from the console
toServer.write(inputLine);
I can send a message to the sever and it will receive it but when I uncomment out this bit for the client to receive a response from the server:
// serverInput = fromServer.readLine();
//
// System.out.println(serverInput);
It will hang and the server side wont receive the initial message sent. I have no idea whats wrong and I just want to get a reply from the server. Any help is appreciated. Thanks
BufferedReader.readLine() will strip the newline character for you.
That means, at client side, inputLine does not have a trailing \n, which means the client did not send the end of line signal to the server and vice versa.
Client Side
toServer.write(...);
toServer.newLine(); // <--- send new line
toServer.flush(); // <--- flush buffered data
Server should do the similar thing.

Receiving Datagram packets are not displayed

I was trying to make a small java program which writes a text message from server to client using DatagramServer and DatagramPacket.
This is the code i've written on the server and client portion.
serverm.java
byte b[] = new byte[1200];
System.out.println("Enter some text");
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String inputMessage = br.readLine();
b = inputMessage.getBytes();
DatagramSocket d = new DatagramSocket(6125);
DatagramPacket p = new DatagramPacket(b,i,InetAddress.getLocalHost(),5461);
d.send(p);
clientm.java
byte b[] = new byte[1024];
try
{
DatagramSocket d = new DatagramSocket(5461);
DatagramPacket p = new DatagramPacket(b,1024);
d.receive(p);
String outputMessage = new String(p.getData(),0,p.getLength());
System.out.println(outputMessage);
}
When running the java program, it runs when the server sends a message to the client - the received message only prints empty line. How can i get the string to be displayed ?
I was able to reproduce your problem when I set the 'i' variable in your server to 0.
Make sure that value is the length of the packet you're sending.

Can't use InputStream from Socket after writeObject

Here is the situation:
I have a ServerSocket ss, and "Socket socket = ss.accept();", then if I do this:
istream = socket.getInputStream();
ostream = socket.getOutputStream();
in = new BufferedReader(new InputStreamReader(istream));
out = new PrintWriter(new BufferedOutputStream(ostream));
/*
I use in/out few times
everything OK
*/
ObjectOutputStream oos = new ObjectOutputStream(ostream);
oos.writeObject(someobject);
/* probably code that solves the problem */
String line = in.readLine();
On the client side I have this code:
PrintWriter out = new PrintWriter(new BufferedOutputStream(socket.getOutputStream()),true);
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
/*
using in/out, no problems
*/
ObjectInputStream ois = new ObjectInputStream(socket.getInputStream());
SomeObject so = (SomeObject)ois.readObject();
out.println("some text");
Everything is OK, until I send someobject. Client recieves object properly, no problems there. But I can't use socket anymore. If I do oos.close(), I get Exception that says "socket closed". If I do oos.reset() I get Exception with similar message. "socket reset". So what should I do? Is it possible to use same input and output streams after writeObject()?
What happens when I send "some text" is that I'm just getting nulls no matter how many times I call readLine(), I never get that "some text".
You can't use multiple type of stream/reader/writer on the same underlying socket. All your streams and readers and writers are buffered so they will all get thoroughly mixed up. Stick tone kind. Stick to one protocol. If you have object streams, use them for everything. And create them once for the life of the socket, not per message.

Client waiting for reply from server though server has sent it

I have a code like the one below
Server side:
Socket socket = server-client conn socket
try
{
BufferedReader inFromNode = new BufferedReader(new InputStreamReader(
socket.getInputStream()));
PrintWriter outToNode = new PrintWriter(socket.getOutputStream(), true);
String data = inFromNode.readLine().toString();
String data1 = inFromNode.readLine().toString();
String data2 = inFromNode.readLine().toString();
outToNode .println("Hi");
}
Client side:
Socket socket = server-client conn socket
try
{
BufferedReader inFromNode = new BufferedReader(new InputStreamReader(
socket.getInputStream()));
PrintWriter outToNode = new PrintWriter(socket.getOutputStream(), true);
outToNode .println("Hi");
outToNode .println("Hi");
outToNode .println("Hi");
String data = inFromNode.readLine().toString();
}
The problem is the client side code is waiting for the reply from the server. I am sure the server side has sent it(I tried placing logs after the send on the server side and they got printed.) Am I overlooking on something here? Is the code wrong in any way?
Try closing the PrintWriter and the Socket when writing to client finishes from Server. This should ideally fix your problem.

Categories

Resources