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


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

Re: Socket problem: read & write to same socket

From Martin Gregorie <martin@address-in-sig.invalid>
Newsgroups comp.lang.java.programmer
Subject Re: Socket problem: read & write to same socket
Date 2012-03-13 21:33 +0000
Organization UK Free Software Network
Message-ID <jjoeec$ts6$1@localhost.localdomain> (permalink)
References <13556496.472.1331654504832.JavaMail.geo-discussion-forums@ynhs12>

Show all headers | View raw


On Tue, 13 Mar 2012 09:01:44 -0700, liyaohua.bupt 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.
>
Your code may accept one connection, read from and write to it, and close 
it, but its not a server because:

(a) it doesn't listen for connections  

(b) it hasn't the right logical structure for accepting more than 
    one connection either serially or in parallel with an existing
    connection

(c) it stops itself without receiving a 'stop' command

If your GO server uses similar logic, frankly I'm not surprised it isn't 
doing anything useful. The logic you've written might function as a 
service under the Unix superserver xinetd, but in that case it would not 
be using a Socket: it would be using System.in and System.out to handle 
messages passed to it via xinetd. 

Anything claims to be a freestanding Java server should be listening for 
connections on a ServerSocket instead of a Socket and, whenever it 
accepts an incoming connection it should do this:

  on connect:
     if connection limit reached
        send a connection rejected message
     else
        spawn a worker thread and pass it the connection
        while connected
           accept input
           validate input
           do work
           send response
 
     close the socket
     terminate the thread
 
The server should keep on listening for and acception connections until 
it is stopped by a command: it should never terminate itself until it 
receives a positive request to do so. 

There are a variety of ways of telling a server to stop, including clock 
watching or monitoring a database, but I normally use a special control 
client that can stop the server, query its status, etc. because this 
reuses the same mechanism as its other clients and so is easy to 
implement.


-- 
martin@   | Martin Gregorie
gregorie. | Essex, UK
org       |

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