Navigation:  Programming Cookbook > Sockets Connectivity >

Listening for a Connection with a ServerSocket

Previous pageReturn to chapter overviewNext page

In order to set up a Socket connection, one end of the proposed conversation must request a connection with another. We call the requesting end the client and the other end the server. This nomenclature is only really relevant while the conversation is being set up. Subsequently, both ends of the connection have equal status and can send unsolicited data to each other as they see fit.

For a server to listen for a connection request from a client we use a ServerSocket set up on a particular TCP/IP port on the server machine.

Ports are represented by integers in the range 0-65,535 but normally those below 1024 are reserved for common services such as a web or FTP servers. Therefore, you should usually choose a port number above 1024 for your applications

Examples

In the Server workspace:

"Create a ServerSocket on port 2048"

serverSocket := ServerSocket port: 2048.

 

"Accept a connection on this port - this call will

block until a client connects. The socket that is

returned can then be used to talk to the client"

socketA := serverSocket accept.

 

At this stage, if you display the value of socketA, it will be nil since no connection has yet been established.