Sockets Tutorial PDF file
Sockets Tutorial PDF file Sockets Tutorial PDF file
IMPLEMENTING AN ECHO SERVER.. Program at the “server –end” * Create a stream socket if((sd= socket(AF_INET,SOCK_STREAM,0) ) = = -1) { fprintf(stderr, “cant create a socket”); exit (1); } * Bind some add to the socket bzero((char *)& server ,sizeof(struct sockaddr_in)); server.sin_family =AF_INET; server.sin_port = htons(port); server.sin_addr.s_addr = htons(INADDR_ANY); * Queue up to 5 connect requests listen(sd ,5);
* Code to handle connection req from client if ((new_sd =accept(sd, (struct sockaddr *)&client ,&client_len)) = =-1) { fprintf(stderr, “can’t accept client”); exit(1); } * Reading bytes …. bytes_to_read = MAXBUFLEN; bp =buf ; while ((n= read(new_sd,bp ,bytes_to_read)) >0 ) { bp +=n; bytes_to_read -=n; } write(new_sd, buf , MAXBUFLEN); } * Closing socket connection at client and server close(new_sd); close(sd);
- Page 1 and 2: The Berkely API (Application Protoc
- Page 3 and 4: server Socket() “socket calls for
- Page 5 and 6: Appl 1 Appl 2 socket socket Underly
- Page 7 and 8: SOCKET CALLS FOR CONNECTIONLESS COM
- Page 9 and 10: SOCKET SYSTEMS CALLS( contd…..) i
- Page 11 and 12: MORE SOCKET CALLS ..!! Client estab
- Page 13 and 14: SYS CALLS FOR DATA TRANSFER ..!! Cl
- Page 15: IP ADDRESS MANIPULATION FUNCTIONS F
- Page 19 and 20: * Input from the user .. gets(sbuf)
- Page 21 and 22: Here is a list of links to Sockets
* Code to handle connection req from client<br />
if ((new_sd =accept(sd, (struct sockaddr *)&client ,&client_len)) = =-1)<br />
{ fprintf(stderr, “can’t accept client”);<br />
exit(1); }<br />
* Reading bytes ….<br />
bytes_to_read = MAXBUFLEN; bp =buf ;<br />
while ((n= read(new_sd,bp ,bytes_to_read)) >0 ) {<br />
bp +=n;<br />
bytes_to_read -=n; }<br />
write(new_sd, buf , MAXBUFLEN); }<br />
* Closing socket connection at client and server<br />
close(new_sd);<br />
close(sd);