Extend to socket again.

Source: Internet
Author: User
Tags epoll htons

The following is a Linux and socket chat program I recently studied using C. To learn epoll, epoll has a lot of socket statements, so it jumps to the C side.

Server. c

# Include <stdio. h> # include <stdlib. h> # include <string. h> # include <errno. h> # include <sys/socket. h> # include <ARPA/inet. h> # include <netinet/in. h> # include <sys/types. h> # include <unistd. h> # define buflen 10 # define ser_addr "127.0.0.1" # Define port 8080 # define max_conn 200int main (INT argc, char ** argv) {int sockfd, newfd; struct sockaddr_in s_addr, c_addr; char Buf [buflen]; socklen_t Len; unsigned int por T = 8080, listnum = 200;/* resume socket */If (sockfd = socket (af_inet, sock_stream, 0) =-1) {perror ("socket"); exit (errno);} else {printf ("socket create success! \ N ");}/* set the length of the listener queue */printf (" listnum = % d ", listnum);/* set the Server IP Address */bzero (& s_addr, sizeof (s_addr); s_addr.sin_family = af_inet; s_addr.sin_port = htons (port); printf ("Port = % d \ n", Port); If (ser_addr) latency = inet_addr (ser_addr); else s_addr.sin_addr.s_addr = inaddr_any; printf ("socket_addr.addr = % d", s_addr.sin_addr.s_addr ); /* bind the address and port to the socket */If (BIND (sockfd, (struct sockaddr *) & S _ ADDR, sizeof (struct sockaddr) =-1) {perror ("bind"); exit (errno);} else {printf ("bind success! \ N ");}/* Listen to the local port */If (Listen (sockfd, listnum) =-1) {perror (" listen "); exit (errno );} else printf ("the server is listening! \ N "); While (1) {printf (" ************ chat start ********* \ n "); len = sizeof (struct sockaddr); If (newfd = accept (sockfd, (struct sockaddr *) & c_addr, & Len) =-1) {printf ("A Client Connected \ n"); perror ("accept"); exit (errno);} else printf ("the client chatting with you is: % s: % d \ n ", inet_ntoa (c_addr.sin_addr), ntohs (c_addr.sin_port); While (1) {_ retry: /***** send message ***/bzero (BUF, buflen); printf ("Enter the message of the sender:");/* fgets function: read Buf from stream LEN-1 characters */fgets (BUF, buflen, stdin);/* print the message sent * // fputs (BUF, stdout); If (! Strncasecmp (BUF, "quit", 4) {printf ("server request to terminate the chat! \ N "); break;}/* If the input string is only" \ n ", that is, press enter, then the new input */If (! Strncmp (BUF, "\ n", 1) {printf ("the input character is only a carriage return. This is incorrect !!! \ N "); goto _ retry;}/* If the Buf contains '\ n', use strlen (BUF)-1, remove '\ n' */If (strchr (BUF,' \ n') Len = Send (newfd, Buf, strlen (BUF)-1, 0 ); /* If the Buf does not contain '\ n', use the actual Buf length strlen (BUF) */else Len = Send (newfd, Buf, strlen (BUF), 0 ); if (LEN> 0) printf ("message sent successfully, the number of bytes sent this time is % d \ n", Len); else {printf ("Message failed to be sent! \ N "); break;}/***** receive message ******/bzero (BUF, buflen); Len = Recv (newfd, Buf, buflen, 0); If (LEN> 0) printf ("The message sent from the client is % S. The total number of bytes is % d \ n", Buf, Len ); else {If (LEN <0) printf ("failed to receive message! \ N "); else printf (" the client has exited and the chat has ended! \ N "); break;}/* close the chat socket */close (newfd);/* exit the server */printf (" whether the server exits the program: y-> yes; n-> NO? "); Bzero (BUF, buflen); fgets (BUF, buflen, stdin); If (! Strncasecmp (BUF, "Y", 1) {printf ("server exited! \ N "); break;}/* close the server socket */close (sockfd); Return 0 ;}

Client. c

# Include <stdio. h> # include <stdlib. h> # include <errno. h> # include <string. h> # include <sys/socket. h> # include <sys/types. h> # include <ARPA/inet. h> # include <netinet/in. h> # include <unistd. h> # define buflen 100int main (INT argc, char ** argv) {int sockfd; struct sockaddr_in s_addr; socklen_t Len; unsigned int Port = 8080; char Buf [buflen]; /* resume socket */If (sockfd = socket (af_inet, sock_stream, 0) =-1) {per Ror ("socket"); exit (errno);} else printf ("socket create success! \ N ");/* set the Server IP Address */bzero (& s_addr, sizeof (s_addr); s_addr.sin_family = af_inet; s_addr.sin_port = htons (port ); char * server_addr = "127.0.0.1"; if (inet_aton (server_addr, (struct in_addr *) & Dr) = 0) {perror (server_addr); exit (errno );} printf ("fingerprint = % d", s_addr.sin_addr.s_addr);/* start to connect to the server */If (connect (sockfd, (struct sockaddr *) & s_addr, sizeof (struct sockaddr ))! = 0) {printf ("Connect failed"); perror ("Connect"); exit (errno);} else printf ("Connect success! % D \ n ", errno); printf (" Enter the while loop "); While (1) {printf (" while "); /** receive message **/bzero (BUF, buflen); Len = Recv (sockfd, Buf, buflen, 0); If (LEN> 0) printf ("the message sent by the server is % S. The total number of bytes is % d \ n", Buf, Len); else {printf ("else "); if (LEN <0) printf ("failed to receive message! \ N "); else printf (" the server has exited and the chat has ended! \ N "); break;} _ retry:/*** send message ***/bzero (BUF, buflen); printf (" Enter the message sent to the other party: ");/* fgets function: Read the BUFLEN-1 characters from the stream */fgets (BUF, buflen, stdin);/* print the message sent * // fputs (BUF, stdout); If (! Strncasecmp (BUF, "quit", 4) {printf ("client request to terminate the chat! \ N "); break;}/* If the input string is only '\ n', that is, press enter, re-enter */If (! Strncmp (BUF, "\ n", 1) {printf ("the input character is only a carriage return. This is incorrect !!! \ N "); goto _ retry;} printf (" % s ", Buf);/* If the Buf contains '\ n', use strlen (BUF)-1, remove '\ n' */If (strchr (BUF,' \ n') {printf ("start to send"); Len = Send (sockfd, Buf, strlen (BUF)-1, 0);}/* If the Buf does not contain \ n, use the real length of the Buf strlen (BUF) */else Len = Send (sockfd, buf, strlen (BUF), 0); printf ("Len = % d", Len); If (LEN> 0) printf ("message sent successfully, the total number of bytes sent this time is % d \ n ", Len); else {printf (" message sending failed! \ N "); break;}/* close connection */close (sockfd); Return 0 ;}

  

In addition, I recently read "C and pointer" and read it intermittently. Record an interesting one today

For Constants

Int const;

Const int;

Both statements are normative.

Int * PI; Pi is a common pointer to an integer.

Int const * CPI; PCI is a pointer to an integer constant. You can modify the pointer value but cannot modify the value it points.

Int * const CPI; CPI is a constant pointer to an integer. A pointer is a constant and its value cannot be modified, but you can modify the integer it points.

Int const * const CPCI; both the pointer and the value pointed to by the pointer are constants and cannot be modified.

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.