Você está na página 1de 4

server using Transfer Control (TCP) protocol.

DESCRIPTION: A socket is one endpoint of a two-way communication link between tw o programs running on the network. A socket is bound to a port number so that th e TCP layer can identify the application that data is destined to be sent. A TCP socket is defined as an endpoint for communication. A socket consists of t he pair <IP Address, Port>. A TCP connection consists of a pair of sockets. Sock ets are distinguished by client and server sockets. A server listens on a port, waiting for incoming requests from clients. socket () creates a new socket of a certain socket type, identified by an intege r number, and allocates system resources to it. bind() is typically used on the server side, and associates a socket with a sock et address structure, i.e. a specified local port number and IP address. listen() is used on the server side, and causes a bound TCP socket to enter list ening state. connect() is used on the client side, and assigns a free local port number to a socket. In case of a TCP socket, it causes an attempt to establish a new TCP con nection. accept() is used on the server side. It accepts a received incoming attempt to c reate a new TCP connection from the remote client, and creates a new socket asso ciated with the socket address pair of this connection. send() and recv(), or write() and read(), or recvfrom() and sendto(), are used f or sending and receiving data to/from a remote socket. close() causes the system to release resources allocated to a socket. In case of TCP, the connection is terminated. gethostbyname() and gethostbyaddr() are used to resolve host names and addresses . select() is used to prune a provided list of sockets for those that are ready to read, ready to write or have errors poll() is used to check on the state of a socket. The socket can be tested to se e if can be written to, read from or has errors. ALGORITHM: TCP SERVER: Step 1: start Step 2: create a socket with address family AF_INET, type SOCK_STREAM and defaul t protocol. Step 3: Initialize the socket and set its attributes assign any port number as d esired. Step 4: Bind the server to the socket using bind() function. Step 5: Establish the listen queue using the listen function. Step 6: wait for client program on request, establish a connection using accept function. Step 7: Repeat steps 8-10 until the server sends bye. Step 8: Read the message from the client using read() function. display the mess age. Step 9: If the client message is bye go to step 11. Step 10: Accept the server message and write it to client using write() function . Step 11: close the connection. Step 12: Go to step 6. TCP CLIENT: Step 1: Start. Step 2: Create a socket with address family AF_INET type SOCK_STERAM and default protocol. Step 3: Initialize the socket and set it attributes set the required port number . Step 4: Connect to the server using connect() function to initialize the request .

Step Step n. Step Step age. Step

5: Repeat steps 6-8 until server sends bye. 6: Accept the client message and write it to the server using write functio 7: If the client message is bye go to step 9. 8: Read the message from the server using read() function. Display the mess 9: stop.

PROGRAM: TCP SERVER: #include<stdio.h> #include<sys/types.h> #include<netinet/in.h> #include<string.h> main() { int sd,sd2,nsd,clilen,sport,len; char sendmsg[20],rcvmsg[20]; struct sockaddr_in servaddr,cliaddr; printf("Enter the Server port"); printf("\n_____________________\n"); scanf("%d",&sport); sd=socket(AF_INET,SOCK_STREAM,0); if(sd<0) printf("Can't Create \n"); else printf("Socket is Created\n"); servaddr.sin_family=AF_INET; servaddr.sin_addr.s_addr=htonl(INADDR_ANY); servaddr.sin_port=htons(sport); sd2=bind(sd,(struct sockaddr*)&servaddr,sizeof(servaddr)); if(sd2<0) printf(" Can't Bind\n"); else printf("\n Binded\n"); listen(sd,5); clilen=sizeof(cliaddr); nsd=accept(sd,(struct sockaddr*)&cliaddr,&clilen); if(nsd<0) printf("Can't Accept\n"); else printf("Accepted\n"); printf("\nReceived Messages\n"); do { recv(nsd,rcvmsg,20,0); printf("%s",rcvmsg); fgets(sendmsg,20,stdin); len=strlen(sendmsg); sendmsg[len-1]='\0'; send(nsd,sendmsg,20,0); wait(20); } while(strcmp(sendmsg,"bye")!=0); }

TCP CLIENT: #include<stdio.h> #include<sys/types.h> #include<netinet/in.h> main() { int csd,cport,len; char sendmsg[20],revmsg[20]; struct sockaddr_in servaddr; printf("Enter the port\n"); scanf("%d",&cport); csd=socket(AF_INET,SOCK_STREAM,0); if(csd<0) printf("Can't Create\n"); else printf("Scocket is Created\n"); servaddr.sin_family=AF_INET; servaddr.sin_addr.s_addr=htonl(INADDR_ANY); servaddr.sin_port=htons(cport); if(connect(csd,(struct sockaddr*)&servaddr,sizeof(servaddr))<0) printf("Can't Connect\n"); else printf("Connected\n"); do { fgets(sendmsg,20,stdin); len=strlen(sendmsg); sendmsg[len-1]='\0'; send(csd,sendmsg,20,0); wait(20); recv(csd,revmsg,20,0); printf("%s",revmsg); } while(strcmp(revmsg,"bye")!=0); }

RESULT: Thus the c program for implementation of TCP was written, exec uted and verified.

Você também pode gostar