Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #12975

Re: Socket problem: read & write to same socket

Path csiph.com!usenet.pasdenom.info!news.chainon-marquant.org!nntpfeed.proxad.net!proxad.net!feeder2-2.proxad.net!82.197.223.106.MISMATCH!feeder1.cambriumusenet.nl!feed.tweaknews.nl!209.197.12.246.MISMATCH!nx02.iad01.newshosting.com!newshosting.com!69.16.185.16.MISMATCH!npeer02.iad.highwinds-media.com!news.highwinds-media.com!feed-me.highwinds-media.com!post01.iad.highwinds-media.com!newsfe15.iad.POSTED!83aa503d!not-for-mail
From Daniel Pitts <newsgroup.nospam@virtualinfinity.net>
User-Agent Mozilla/5.0 (Macintosh; Intel Mac OS X 10.6; rv:10.0.2) Gecko/20120216 Thunderbird/10.0.2
MIME-Version 1.0
Newsgroups comp.lang.java.programmer
Subject Re: Socket problem: read & write to same socket
References <13556496.472.1331654504832.JavaMail.geo-discussion-forums@ynhs12> <4f5f726c$0$290$14726298@news.sunsite.dk> <13316031.394.1331656218306.JavaMail.geo-discussion-forums@ynes7>
In-Reply-To <13316031.394.1331656218306.JavaMail.geo-discussion-forums@ynes7>
Content-Type text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding 8bit
Lines 74
Message-ID <wuL7r.63388$rS.46212@newsfe15.iad> (permalink)
X-Complaints-To abuse@newsrazor.net
NNTP-Posting-Date Tue, 13 Mar 2012 17:30:04 UTC
Date Tue, 13 Mar 2012 10:30:03 -0700
X-Received-Bytes 3525
Xref csiph.com comp.lang.java.programmer:12975

Show key headers only | View raw


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
>

Back to comp.lang.java.programmer | Previous | NextPrevious in thread | Next in thread | Find similar


Thread

Socket problem: read & write to same socket liyaohua.bupt@gmail.com - 2012-03-13 09:01 -0700
  Re: Socket problem: read & write to same socket Arne Vajhøj <arne@vajhoej.dk> - 2012-03-13 12:14 -0400
    Re: Socket problem: read & write to same socket liyaohua.bupt@gmail.com - 2012-03-13 09:30 -0700
      Re: Socket problem: read & write to same socket Daniel Pitts <newsgroup.nospam@virtualinfinity.net> - 2012-03-13 10:30 -0700
  Re: Socket problem: read & write to same socket Martin Gregorie <martin@address-in-sig.invalid> - 2012-03-13 21:33 +0000
    Re: Socket problem: read & write to same socket Arne Vajhøj <arne@vajhoej.dk> - 2012-03-13 17:48 -0400
      Re: Socket problem: read & write to same socket Martin Gregorie <martin@address-in-sig.invalid> - 2012-03-13 22:03 +0000
        Re: Socket problem: read & write to same socket Arne Vajhøj <arne@vajhoej.dk> - 2012-03-13 18:25 -0400
          Re: Socket problem: read & write to same socket Lew <lewbloch@gmail.com> - 2012-03-13 15:38 -0700
            Re: Socket problem: read & write to same socket Arne Vajhøj <arne@vajhoej.dk> - 2012-03-13 18:47 -0400
              Re: Socket problem: read & write to same socket Martin Gregorie <martin@address-in-sig.invalid> - 2012-03-13 23:33 +0000
                Re: Socket problem: read & write to same socket Arne Vajhøj <arne@vajhoej.dk> - 2012-03-13 19:35 -0400
                Re: Socket problem: read & write to same socket Paul Cager <paul.cager@googlemail.com> - 2012-03-14 02:51 -0700
                Re: Socket problem: read & write to same socket Martin Gregorie <martin@address-in-sig.invalid> - 2012-03-15 00:45 +0000
                Re: Socket problem: read & write to same socket Paul Cager <paul.cager@googlemail.com> - 2012-03-15 03:18 -0700
                Re: Socket problem: read & write to same socket RedGrittyBrick <RedGrittyBrick@spamweary.invalid> - 2012-03-14 16:22 +0000
                Re: Socket problem: read & write to same socket glen herrmannsfeldt <gah@ugcs.caltech.edu> - 2012-03-15 01:33 +0000
                Re: Socket problem: read & write to same socket Nigel Wade <nmw@le.ac.uk> - 2012-03-15 12:30 +0000
          Re: Socket problem: read & write to same socket Martin Gregorie <martin@address-in-sig.invalid> - 2012-03-13 23:00 +0000
            Re: Socket problem: read & write to same socket Arne Vajhøj <arne@vajhoej.dk> - 2012-03-13 19:36 -0400

csiph-web