Some Techniques of Socket Programming That You Should Know About 0. You know that in TCP the active closing agent waits 1-2 minutes in the TIME_WAIT state before closing entirely. When we test our programs, however, it can become cumbersome to have to wait 2 minutes before we can bind to the same port again (on the server side). It is possible to set a "socket option" to avoid the TIME_WAIT state. Obviously, this should only be done during testing: in Java: ServerSocket sfd = new ... sfd.setReuseAddress(true); in C: int x = 1; setsockopt(sfd,SOL_SOCKET,SO_REUSEADDR,&x,sizeof(int)); 1. What happens if you try to connect to a server port that doesn't exist, or is not ready to accept() your connection? In Java, it will raise an exception. In C, the "connect" call will return a non-zero value (0 means it succeeded). So, in Java, you can do the following to keep trying to connect until you succeed: Socket cfd; boolean tryagain = true; while (tryagain) { try { cfd = new Socket(servername,serverport); tryagain = false; } catch (Exception ee) { tryagain = true; try {Thread.sleep(1000);} catch (Exception e) {} } } The "Thread.sleep" line forces you to wait 1 second between connection attempts. Otherwise you may inadvertently launch a DOS attack. if C, you can just check the value returned by connect(); 2. When you're waiting to read from the socket, sometimes you want to set a timeout value so that after a while you decide that something's wrong, and stop waiting. This is in fact a very important capability, since otherwise a bad remote program can cause your program to hang indefinitely. Setting timeouts on reads in Java is rather easy: cfd.setSoTimeout(30000); // sets 30000 millisecond (30 second) timeout // can do same for Socket and ServerSocket Now if a read on cfd times out, Java throws a SocketTimeoutException. So you can do: try { ... } catch (SocketTimeoutException ste) {...} With C, you'll have to be familiar with elements of Unix system programming. The operation to use is "select", which is in fact a very powerful system call, and among the things you can do is to use it to cause your program to hang until a file descriptor (socket) becomes readable for a given amount of time. I've written the following function, which you can use. If it returns a positive value, then the fd is now readable. But if it returns 0, then the specified time in seconds has ran out. Use as in: if (readable_timeo(cfd,30) > 0) { cfd.read(...) } else { printf("timed out!"); ... } #include ... // returns -1 on error, 0 on timeout int readable_timeo(int fd, int sec) { fd_set rset; struct timeval tv; FD_ZERO(&rset); FD_SET(fd,&rset); tv.tv_sec = sec; tv.tv_usec = 0; return (select(fd+1,&rset,NULL,NULL,&tv)); } // readable_timeo 3. Reading ALL the Information. When you call "read", you might not get as much as you hope. Because the TCP agent may have to break the data into multiple segments (according to the MSS or Nagle's algorithm, or when there's network congestion, for example), if there's 512 bytes to send it might in fact be sent in two smaller segments (this is unlikely since MSS is usually 1500 bytes - the MTU of most networks, but for larger packets it could happen). To make sure you read all the information that was sent, you may have to call multiple reads. The following function will help you do that: in Java: static void readFully(DataInputStream din, byte[] buf) throws Exception { int len = buf.length; int br; // bytes read each time; int total = 0; while (total ... char * hostname; // assume holds host name such as "mothership" char * ipaddress; // to point to ip address string such as "10.1.0.7" ... struct hostent *hostinfo; hostinfo = gethostbyname(*hostname); if (!hostinfo) { printf("host lookup failed\n"); exit(1); } ipaddress = inet_ntoa(*(struct in_addr *)*(hostinfo->h_addr_list)); You can now use ipaddress when constructing a sockaddr_in: saddr.sin_addr.s_addr = inet_addr(ipaddress); 5. Turning off Nagel's algorithm: in C: int x = 1; setsockopt(sfd,SOL_SOCKET,TCP_NODELAY,&x,sizeof(int)); in Java: Give Socket cfd: cfd.setTcpNoDelay(true); When should you use this option? Almost never. You need to understand how Nagel's algorithm works. Interactive applications typically do NOT require turning off the option. ---- You are expected to consult C/Unix/Java documentation for other details.