Java forum for Java programmers

Socket problem: read & write to same socket

Mar 13, 2012 4:01 pm
Guest

I want to establish connection to a server(written by myself in Go language), read from socket, and then write into socket.

The connection can be established, and it reads correctly. But after that and when I want to write to socket, it closes the connection. I used wireshark to listen to the packets. I saw my program sent a FIN to the server side. So the server receives nothing.

Note that the server side only sends one line into socket.

I later wrote a server in Java and a client in Go. They work fine in both read and write.

Thanks in advance!



import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;



public class DeserializerTester {

/**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
Socket s = null;
BufferedReader in = null;
BufferedWriter out = null;
//PrintWriter out = null;

try {
s = new Socket("127.0.0.1", 9999);
in = new BufferedReader(new InputStreamReader(s.getInputStream()));
//out = new PrintWriter(s.getOutputStream(), false);
out = new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
} catch (UnknownHostException e) {
System.err.println("Unknown host");
System.exit(0);
} catch (IOException e) {
System.err.println("IO error");
System.exit(1);
}

String msg = "";

msg = in.readLine();
System.out.println(msg);

out.write("\"hi, socket\"");
s.close();
}

}

Mar 13, 2012 4:14 pm
Arne Vajhøj
Re: Socket problem: read & write to same socket

On 3/13/2012 12:01 PM, liyaohua.bupt@gmail.com wrote:
> I want to establish connection to a server(written by myself in Go language), read from socket, and then write into socket.
>
> The connection can be established, and it reads correctly. But after that and when I want to write to socket, it closes the connection. I used wireshark to listen to the packets. I saw my program sent a FIN to the server side. So the server receives nothing.
>
> Note that the server side only sends one line into socket.
>
> I later wrote a server in Java and a client in Go. They work fine in both read and write.

> import java.io.BufferedReader;
> import java.io.BufferedWriter;
> import java.io.IOException;
> import java.io.InputStreamReader;
> import java.io.OutputStreamWriter;
> import java.io.PrintWriter;
> import java.net.Socket;
> import java.net.UnknownHostException;
>
> public class DeserializerTester {
> public static void main(String[] args) throws IOException {
> // TODO Auto-generated method stub
> Socket s = null;
> BufferedReader in = null;
> BufferedWriter out = null;
> //PrintWriter out = null;
>
> try {
> s = new Socket("127.0.0.1", 9999);
> in = new BufferedReader(new InputStreamReader(s.getInputStream()));
> //out = new PrintWriter(s.getOutputStream(), false);
> out = new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
> } catch (UnknownHostException e) {
> System.err.println("Unknown host");
> System.exit(0);
> } catch (IOException e) {
> System.err.println("IO error");
> System.exit(1);
> }
>
> String msg = "";
>
> msg = in.readLine();
> System.out.println(msg);
>
> out.write("\"hi, socket\"");

Try:

out.flush();

here.

> s.close();
> }
>
> }

Arne


Mar 13, 2012 4:30 pm
Guest
Re: Socket problem: read & write to same socket

It works! Thanks! It busted me for quite a while.

On Tuesday, March 13, 2012 11:14:36 AM UTC-5, Arne Vajhøj wrote:
> On 3/13/2012 12:01 PM, liyaohua.bupt@gmail.com wrote:
> > I want to establish connection to a server(written by myself in Go language), read from socket, and then write into socket.
> >
> > The connection can be established, and it reads correctly. But after that and when I want to write to socket, it closes the connection. I used wireshark to listen to the packets. I saw my program sent a FIN to the server side. So the server receives nothing.
> >
> > Note that the server side only sends one line into socket.
> >
> > I later wrote a server in Java and a client in Go. They work fine in both read and write.
>
> > import java.io.BufferedReader;
> > import java.io.BufferedWriter;
> > import java.io.IOException;
> > import java.io.InputStreamReader;
> > import java.io.OutputStreamWriter;
> > import java.io.PrintWriter;
> > import java.net.Socket;
> > import java.net.UnknownHostException;
> >
> > public class DeserializerTester {
> > public static void main(String[] args) throws IOException {
> > // TODO Auto-generated method stub
> > Socket s = null;
> > BufferedReader in = null;
> > BufferedWriter out = null;
> > //PrintWriter out = null;
> >
> > try {
> > s = new Socket("127.0.0.1", 9999);
> > in = new BufferedReader(new InputStreamReader(s.getInputStream()));
> > //out = new PrintWriter(s.getOutputStream(), false);
> > out = new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
> > } catch (UnknownHostException e) {
> > System.err.println("Unknown host");
> > System.exit(0);
> > } catch (IOException e) {
> > System.err.println("IO error");
> > System.exit(1);
> > }
> >
> > String msg = "";
> >
> > msg = in.readLine();
> > System.out.println(msg);
> >
> > out.write("\"hi, socket\"");
>
> Try:
>
> out.flush();
>
> here.
>
> > s.close();
> > }
> >
> > }
>
> Arne



Mar 13, 2012 5:30 pm
Daniel Pitts
Re: Socket problem: read & write to same socket

Liyaohua, please realize the reason is that you use a BufferedWriter,
and you close the socket directly. The BufferedWriter has no way to
hook into that close and flush itself. out.close instead of s.close()
would achieve the same thing. Beware though that sometimes you don't
want to close a socket just because you're done with the OutputStream
(or InputStream).

Sorry to top post but I'm afraid the OP might not see this comment
otherwise...

On 3/13/12 9:30 AM, liyaohua.bupt@gmail.com wrote:
> It works! Thanks! It busted me for quite a while.
>
> On Tuesday, March 13, 2012 11:14:36 AM UTC-5, Arne Vajhøj wrote:
>> On 3/13/2012 12:01 PM, liyaohua.bupt@gmail.com wrote:
>>> I want to establish connection to a server(written by myself in Go language), read from socket, and then write into socket.
>>>
>>> The connection can be established, and it reads correctly. But after that and when I want to write to socket, it closes the connection. I used wireshark to listen to the packets. I saw my program sent a FIN to the server side. So the server receives nothing.
>>>
>>> Note that the server side only sends one line into socket.
>>>
>>> I later wrote a server in Java and a client in Go. They work fine in both read and write.
>>
>>> import java.io.BufferedReader;
>>> import java.io.BufferedWriter;
>>> import java.io.IOException;
>>> import java.io.InputStreamReader;
>>> import java.io.OutputStreamWriter;
>>> import java.io.PrintWriter;
>>> import java.net.Socket;
>>> import java.net.UnknownHostException;
>>>
>>> public class DeserializerTester {
>>> public static void main(String[] args) throws IOException {
>>> // TODO Auto-generated method stub
>>> Socket s = null;
>>> BufferedReader in = null;
>>> BufferedWriter out = null;
>>> //PrintWriter out = null;
>>>
>>> try {
>>> s = new Socket("127.0.0.1", 9999);
>>> in = new BufferedReader(new InputStreamReader(s.getInputStream()));
>>> //out = new PrintWriter(s.getOutputStream(), false);
>>> out = new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
>>> } catch (UnknownHostException e) {
>>> System.err.println("Unknown host");
>>> System.exit(0);
>>> } catch (IOException e) {
>>> System.err.println("IO error");
>>> System.exit(1);
>>> }
>>>
>>> String msg = "";
>>>
>>> msg = in.readLine();
>>> System.out.println(msg);
>>>
>>> out.write("\"hi, socket\"");
>>
>> Try:
>>
>> out.flush();
>>
>> here.
>>
>>> s.close();
>>> }
>>>
>>> }
>>
>> Arne
>





Previous Thread: [Windows] Any way to distinguish ^C Induced EOF from ^Z EOF?
Next Thread: What are the proper terms for these concepts?

Related Forum Topics
Datagram socket problem
Got a bug somewhere...
Is this bit of code going to cause problems ie the bit running in the
loop? I find that the code never exits ds.receive(incoming)


try
{
DatagramSocket ds = new DatagramSocket(Constants.LOCAL_PORT);
DatagramPacket incoming = new...
Strange Socket problem
I'm having a problem in some production code that I can't figure out.
I'll post the complete actual code below. This code is running in three
places and has the same problem in two of them at the same time. The
other I'm not sure, it may be that the personnel operating it are
restarting...
Strange Socket Problem Solved!
I want to thank everybody that gave me ideas to help me find my problem
with my program. It turns out that the problem was bad data throwing a
RuntimeException in the thread, bringing it to a stop. I probably
should have programmed it with bad data in mind but I didn't. We had an...
Detection of socket connecting process, two thread problem
Hi,
I use one socket and two threads with it: one for reading and one for writing to it. My problem is in moment when error occurs and two thread want to do connection again on the same socket by closing the secont at start. Result of this is that while one one thread (the firtst of catching...
I need to write Simple JAVA program to read and write from USB serialto use it with Arduino
Hi every one

I’m trying to write simple JAVA program that could send command to Arduino board or read output from Arduino.
I tried java-simple-serial-connector library but I keep getting error.
And in Arduino web site they use gnu.io. classpath witch I have no idea how to use it with java...
Setting TCP parameters for Socket?
Sahm's post reminded me about this:

Investigating Socket::isReachable() a while back, I discovered that the
Socket constructor actually establishes a TCP connection. This means
you can't set TCP parameters for the inital connection. For example,
SO_TIMEOUT.

Socket sock = new...
Detecting Socket disconnect
I have a java.net.Socket that I only write to, and I can't figure out how to tell if it's disconnected.

I call socket.isConnected(), but it still returns true after I broke the TCP connection. I had telnetted in thru Windows telnet and closed the window.

Anyone know how to detect the...
Which is best java library for socket programming
Please let me know which is best java library for socket programming ?


Java (android) socket reconnection
Hi,
Could somebody help me resolve I think small problem for You but huge for me.
How to correctly support reconnecting client sockets.
I have one thread (it's name thrd1) for controlling connection (and reconnection if is it needed) and thread (it's name thrd2) for cyclical sending data to...
Append() vs. write()
Hi,

I want to write a method that can write possibly large matrices of
floating point numbers in scientific notation to human readable ascii
files. I want to be able to specify the formatting of the floating
point numbers usins format strings like "%1.8e" or "%2.5f".

From what I take...