Penguin
Note: You are viewing an old revision of this page. View the current version.

This is to describe how to write networking programs in C using the BSD Socket Layer API. This mostly applies to almost all other languages with only minor modifications.

How to create a TCP Server

1
TCP is a streaming socket from the internet family of protocols, so first we create a socket of the correct type:

struct protoent *protocol; int serverfd;

protocol=getprotobyname("tcp"); if (!protocol) {

printf("Could not getprotobyname");

exit(1)?;

} server=socket(PF_INET,SOCK_STREAM,protocol.p_proto); if (server<0) {

perror("socket");

exit(2);

}

2
Then we create an address to bind it to. We don't care which of our local IP's we bind it to so we choose "INADDR_ANY" (all interfaces)

struct sockaddr_in address; address.sin_family = AF_INET; address.sin_port = htons(port goes here) address.sin_addr.s_addr = INADDR_ANY;

3
Then we bind the port to that socket.

if (bind(serverfd,&address,sizeof(address))<0) {

perror("bind");

exit(3);

}

4
Now we start listening on that port. The 5 is the number of outstanding connections that will be queued by the kernel before you get a chance to accept(2) them.

if (listen(serverfd,5)<0) {

perror("listen");

exit(4)?;

}

5
And now we wait for an incoming connection

int sockfd; struct sockaddr_in clientaddress;

sockfd=accept(serverfd,&clientaddress,sizeof(clientaddress));

if (sockfd<0) {

perror("accept");

exit(5)?;

}

This creates a new socket (called "sockfd") that we can talk to the client that connected with. We can go back and do the accept(2) again to get another client socket etc.

How to create a TCP Client

1
First you need to create a socket as in TCP Server step 1 above, note the rest of this example uses "sockfd" not "serverfd"
2
Optionally you can bind the socket to a local address using step 2 and 3 above. Note, this isn't necessary, or usually even desired.
3
Next you want to create an address to connect out to:

struct sockaddr_in address;

address.sin_family = AF_INET; address.sin_port = htons(port goes here);

struct hostent *host; int i=0; int success=0; host = gethostbyname("host.name.goes.here"); if (!host) {

fprintf(stderr,"gethostbyname: %s",hstrerror(herrno));

exit(5)?;

} for (i=0;i<host->h_length;i++) {

if(connect(sockfd,&address,sizeof(address))==0) {

success=1; break;

} fprintf(stderr,"connect(%s):%s",inet_ntoa(address.sin_addr),strerror(errno));

} if (!success) {

fprintf(stderr,"hostname is unreachable");

exit(6)?;

}

Done!

How to talk over a TCP socket.

Now, after you have TCP connection, either by having accept(2) or connect(2) return, you want to send traffic over it.

!!To Read Data

char buffer[65535?; int bytes; bytes=read(sockfd,buffer,sizeof(buffer)); if (bytes<0) {

perror("read");

} else if (bytes==0) {

close(sockfd); printf("Socket closed"); exit(0);

} process data

The number of bytes read is in "bytes", if bytes is 0, then the other end closed the connection.

To Write Data

char buffer[65535?; int bytes; int remaining; populate buffer with data, set bytes to being the number of bytes to write, eg: strcpy(buffer,"Hello World"); bytes=strlen(buffer); remaining=bytes; while(remaining>0) {

ret=write(sockfd,buffer+(bytes-remaining),remaining); if (ret<0) {

perror("write"); close(ret);

exit(8)?;

} remaining=remaining-ret;

}

How to write a UDP Server

(To be written)

How to write a UDP Client

(To be written)

Differences between TCP/UDP (ip(7)) and Unix Domain Sockets (unix(7))

(To be written)

lib/BlockParser.php:505: Notice: Undefined property: _tight_top