C socket demo

Source: Internet
Author: User

I. Server-server. c

# Include <stdio. h> # include <sys/socket. h> # include <ARPA/inet. h> # include <stdlib. h> # include <string. h> # include <unistd. h> # include <netinet/in. h> # define maxpending 5 # define buffsize 32 void die (char * mess) {perror (MESS); exit (1);} void handleclient (INT sock) {char buffer [buffsize]; int writable ED =-1; if (cached ED = Recv (sock, buffer, buffsize, 0) <0) {die ("failed to recevie in1_bytes from client ");} While (bounded ed> 0) {If (send (sock, buffer, cached ed, 0 )! = Committed ed) {die ("failed to send bytes to client");} If (cached ED = Recv (sock, buffer, buffsize, 0) <0) {die ("failed to receive additional bytes from client") ;}} close (sock) ;}int main (INT argc, char * argv []) {int serversock, clientsock; struct sockaddr_in echoserver, echoclient; If (argc! = 2) {fprintf (stderr, "Usage: echoserver <port>/N"); exit (1) ;}if (serversock = socket (pf_inet, sock_stream, ipproto_tcp )) <0) {die ("failed to create socket");} memset (& echoserver, 0, sizeof (echoserver); echoserver. sin_family = af_inet; echoserver. sin_addr.s_addr = htonl (inaddr_any); echoserver. sin_port = htons (atoi (argv [1]); If (BIND (serversock, (struct sockaddr *) & echoserver, sizeof (echoserver) <0) {die ("failed to bind the server socket");} If (Listen (serversock, maxpending) <0) {die ("failed to listen on server socket ");} while (1) {unsigned int clientlen = sizeof (echoclient); If (clientsock = accept (serversock, (struct sockaddr *) & echoclient, & clientlen) <0) {die ("failed to accept client connection");} fprintf (stdout, "Client Connected: % s/n", inet_ntoa (echoclient. sin_addr); handleclient (clientsock );}} 

 

Ii. Client-client. c

# Include <stdio. h> # include <sys/socket. h> # include <ARPA/inet. h> # include <stdlib. h> # include <string. h> # include <unistd. h> # include <netinet/in. h> # define buffsize 32 # define FP fprintfvoid die (char * mess) {perror (MESS); exit (1);} int main (INT argc, char * argv []) {int sock; struct sockaddr_in echoserver; char buffer [buffsize]; unsigned int echolen; int received = 0; If (argc! = 4) {FP (stderr, "Usage: TCP echo <server_ip> <word> <port>/N"); exit (1 );} if (sock = socket (pf_inet, sock_stream, ipproto_tcp) <0) {die ("failed to create socket");} memset (& echoserver, 0, sizeof (echoserver); echoserver. sin_family = af_inet; echoserver. sin_addr.s_addr = inet_addr (argv [1]); echoserver. sin_port = htons (atoi (argv [3]); If (connect (sock, (struct sockaddr *) & echoserver, sizeof (Echoserver) <0) {die ("failed to connect with server");} echolen = strlen (argv [2]); If (send (sock, argv [2], echolen, 0 )! = Echolen) {die ("mismatch in number of sent bytes");} FP (stdout, "Received:"); While (received <echolen) {int bytes = 0; if (Bytes = Recv (sock, buffer, BUFFSIZE-1, 0) <1) {die ("failed to receive bytes from server");} received + = bytes; buffer [bytes] = '/0'; FP (stdout, buffer);} FP (stdout, "/N"); close (sock); exit (0 );} 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.