Mac OS X socket 2 [a simple example]

Source: Internet
Author: User
Tags htons

Mac OS X socket 2 [a simple example]

Xcode-> new Workspace

Xcode-> new project: C: [add to:?]

Server:

Code

//// Main. C // S2 // server // created by DMD on 4/7/14. # include <stdio. h> # include <sys/types. h> # include <sys/socket. 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 12345 # define queue 20 # define buffer_size 1024 // severint main (INT argc, const char * argv []) {printf ("\ nserver: \ n "); // defines sockfd in T server_sockfd = socket (af_inet, sock_stream, 0); If (-1 = server_sockfd) {printf ("initialize socket fail! \ R \ n "); Return-1;} printf (" initialize socket OK! \ R \ n "); // defines sockaddr_in struct sockaddr_in server_sockaddr; outputs = af_inet; outputs = htons (myport); outputs = htonl (inaddr_any); // bind, if (BIND (server_sockfd, (struct sockaddr *) & server_sockaddr, sizeof (server_sockaddr) =-1) {perror ("bind "); exit (1);} printf ("bind OK! \ R \ n "); // listen, 0 is returned for success, and-1 If (Listen (server_sockfd, queue) =-1) is returned for error) {perror ("listen"); exit (1);} printf ("Starting Listen ...... \ r \ n "); printf (" (please run client and input something) \ r \ n "); // client socket char buffer [buffer_size]; char send_client_buffer [buffer_size]; struct sockaddr_in client_addr; socklen_t length = sizeof, (struct so Ckaddr *) & client_addr, & length); If (conn <0) {perror ("Accept connect failed"); exit (1 );} printf ("START accept data from client... \ r \ n "); While (1) {memset (buffer, 0, sizeof (buffer); memset (send_client_buffer, 0, sizeof (send_client_buffer )); // start to accept the message from the server int Len = Recv (Conn, buffer, sizeof (buffer), 0); // printf ("client said, \ "% s \" \ n ", buffer); printf (" client said: % s ", buffer); // If you enter" exit ", the server will jump out of the while, That is, no longer accept data from the client, and disable the server if (strcmp (buffer, "Exit \ n") = 0) {break;} else if (strcmp (buffer, "100 \ n") = 0) {sprintf (send_client_buffer, "Hello, this is One handred: % s", buffer);} else if (strcmp (buffer, "30 \ n") = 0) {sprintf (send_client_buffer, "Hello, this is thirty: % s", buffer);} else {sprintf (send_client_buffer, "server repeat you said that is: % s", buffer); // output information to the screen // fputs (send_client_buffer, stdou T);} // send the string buffer to the conn of the client. Send (Conn, send_client_buffer, strlen (send_client_buffer), 0); memset (buffer, 0, sizeof (buffer); memset (send_client_buffer, 0, sizeof (send_client_buffer ));} // close the connection close (conn); close (server_sockfd); Return 0;} // end


Xcode-> new project: C:

Client:

Code

//// Main. C // C2 // client // created by DMD on 4/7/14. # 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 12345 # define buffer_size 1024 // Add a parameter to the main function to obtain an unknown character during the first input. // Int main (INT argc, const char * argv []) int main () {printf ("\ nclient: \ n "); /// define sockfd int sock_cli = socket (af_inet, sock_stream, 0); If (-1 = sock_cli) {printf ("initialize socket fail! \ R \ n "); Return-1;} // printf (" initialize socket OK! \ R \ n "); // 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) ;}printf ("Success connect Server IP: % d, Port: % D! \ R \ n ", servaddr. sin_addr.s_addr, servaddr. sin_port); printf ("please type something to server: \ r \ n"); char sendbuf [buffer_size]; char recvbuf [buffer_size]; memset (sendbuf, 0, sizeof (sendbuf); memset (recvbuf, 0, sizeof (recvbuf); // client: Enter the string to sendbuf and press enter to end while (fgets (sendbuf, sizeof (sendbuf), stdin )! = NULL) {// send data to the server side send (sock_cli, sendbuf, strlen (sendbuf), 0); // send // If the input is exit, exit if (strcmp (sendbuf, "Exit \ n") = 0) {break;} // receives the feedback from the server. Recv (sock_cli, recvbuf, sizeof (recvbuf), 0); // receive // output information to the screen // fputs (recvbuf, stdout); printf ("% s", recvbuf ); memset (sendbuf, 0, sizeof (sendbuf); memset (recvbuf, 0, sizeof (recvbuf);} Close (sock_cli); Return 0 ;}


Test:

Step1.run Server

Step2.run Client

In client type:

100 enter

30 enter

Hello


Success!

Schematic diagram: communication between the server and the client


End

By DMD

Related Article

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.