Linuxcsocket source code case

Source: Internet
Author: User

Linuxcsocket source code case

Service end
# Include <sys/types. h> # include <sys/socket. h> # include <stdio. h> # include <netinet/in. h> # include <arpa/inet. h> # include <unistd. h> # include <string. h> # include <stdlib. h> # include <fcntl. h> # include <sys/shm. h> # define MYPORT 8887 # define QUEUE 20 # define BUFFER_SIZE 1024int main () {// defines sockfd int server_sockfd = socket (AF_INET, SOCK_STREAM, 0 ); /// define sockaddr_in struct sockaddr_in server_sockaddr; server_sockaddr.sin_family = AF_INET; outputs = htons (MYPORT); outputs = htonl (INADDR_ANY); // bind, 0 is returned successfully, -1 if (bind (server_sockfd, (struct sockaddr *) & server_sockaddr, sizeof (server_sockaddr) =-1) {perror ("bind "); exit (1);} // listen, 0 is returned for success, and-1 if (listen (server_sockfd, QUEUE) =-1) {perror ("listen") is returned for error "); exit (1) ;}/// client socket char buffer [BUFFER_SIZE]; struct sockaddr_in client_addr; socklen_t length = sizeof (client_addr); // a non-negative descriptive word is returned successfully, -1 int conn = accept (server_sockfd, (struct sockaddr *) & client_addr, & length); if (conn <0) {perror ("connect "); exit (1);} while (1) {memset (buffer, 0, sizeof (buffer); int len = recv (conn, buffer, sizeof (buffer), 0 ); if (strcmp (buffer, "exit \ n") = 0) break; fputs (buffer, stdout); send (conn, buffer, len, 0 );} close (conn); close (server_sockfd); return 0 ;}
Client
# Include <sys/types. h> # include <sys/socket. h> # include <stdio. h> # include <netinet/in. h> # include <arpa/inet. h> # include <unistd. h> # include <string. h> # include <stdlib. h> # include <fcntl. h> # include <sys/shm. h ># define MYPORT 8887 # define BUFFER_SIZE 1024int main () {// defines sockfd int sock_cli = socket (AF_INET, SOCK_STREAM, 0); // defines sockaddr_in struct sockaddr_in servaddr; memset (& servaddr, 0, sizeof (servaddr )); Servaddr. sin_family = AF_INET; servaddr. sin_port = htons (MYPORT); // server port servaddr. sin_addr.s_addr = inet_addr ("127.0.0.1"); // server ip // connect to the server, 0 is returned for success, and-1 is returned for error if (connect (sock_cli, (struct sockaddr *) & servaddr, sizeof (servaddr) <0) {perror ("connect"); exit (1) ;}char sendbuf [BUFFER_SIZE]; char recvbuf [BUFFER_SIZE]; while (fgets (sendbuf, sizeof (sendbuf), stdin )! = NULL) {send (sock_cli, sendbuf, strlen (sendbuf), 0); // send if (strcmp (sendbuf, "exit \ n") = 0) break; recv (sock_cli, recvbuf, sizeof (recvbuf), 0); // receives fputs (recvbuf, stdout); memset (sendbuf, 0, sizeof (sendbuf )); memset (recvbuf, 0, sizeof (recvbuf);} close (sock_cli); return 0 ;}

Test:

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.