Path: csiph.com!usenet.pasdenom.info!news.chainon-marquant.org!nntpfeed.proxad.net!proxad.net!feeder1-2.proxad.net!74.125.46.80.MISMATCH!postnews.google.com!glegroupsg2000goo.googlegroups.com!not-for-mail From: liyaohua.bupt@gmail.com Newsgroups: comp.lang.java.programmer Subject: Re: Socket problem: read & write to same socket Date: Tue, 13 Mar 2012 09:30:18 -0700 (PDT) Organization: http://groups.google.com Lines: 69 Message-ID: <13316031.394.1331656218306.JavaMail.geo-discussion-forums@ynes7> References: <13556496.472.1331654504832.JavaMail.geo-discussion-forums@ynhs12> <4f5f726c$0$290$14726298@news.sunsite.dk> NNTP-Posting-Host: 131.193.36.84 Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable X-Trace: posting.google.com 1331657712 19680 127.0.0.1 (13 Mar 2012 16:55:12 GMT) X-Complaints-To: groups-abuse@google.com NNTP-Posting-Date: Tue, 13 Mar 2012 16:55:12 +0000 (UTC) In-Reply-To: <4f5f726c$0$290$14726298@news.sunsite.dk> Complaints-To: groups-abuse@google.com Injection-Info: glegroupsg2000goo.googlegroups.com; posting-host=131.193.36.84; posting-account=NjklqAoAAADSGBlIELlXZb7UZRW3Kyc3 User-Agent: G2/1.0 Xref: csiph.com comp.lang.java.programmer:12966 It works! Thanks! It busted me for quite a while. On Tuesday, March 13, 2012 11:14:36 AM UTC-5, Arne Vajh=F8j 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 lang= uage), read from socket, and then write into socket. > > > > The connection can be established, and it reads correctly. But after th= at and when I want to write to socket, it closes the connection. I used wir= eshark 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 bo= th read and write. >=20 > > 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 =3D null; > > BufferedReader in =3D null; > > BufferedWriter out =3D null; > > //PrintWriter out =3D null; > > > > try { > > s =3D new Socket("127.0.0.1", 9999); > > in =3D new BufferedReader(new InputStreamReader(s.getInputStream()))= ; > > //out =3D new PrintWriter(s.getOutputStream(), false); > > out =3D 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 =3D ""; > > > > msg =3D in.readLine(); > > System.out.println(msg); > > > > out.write("\"hi, socket\""); >=20 > Try: >=20 > out.flush(); >=20 > here. >=20 > > s.close(); > > } > > > > } >=20 > Arne