Mac OS X socket 1 [A simple example]

Source: Internet
Author: User

Mac OS X socket 1 [A simple example]

Server code

C:

//// Main. C // unix_socket_very_simple_server // created by DMD on 4/7/14. /* function: Server for Unix (OS X Mac) */# include <stdio. h> # include <stdlib. h> # include <sys/types. h> // UNIX socket # include <sys/socket. h> # include <netinet/in. h> # include <string. h> // the server and client both use port: 12345 // The server address is 127.0.0.1. You can write any address on the server, but you must write this address on the client. // Because the server address is unknown, int main (INT argc, const char * argv []) cannot be sent to the server. {// listen port [this port must be used by the client] unsigned short portnum = 12345; int SFP, NFP; struct sockaddr_in s_add, c_add; int sin_size; printf ("Starting listen in server: \ r \ n "); // initialize socket SFP = socket (af_inet, sock_stream, 0); // TCP: sock_stream if (-1 = SFP) {printf ("initialize socket fail! \ R \ n "); Return-1;} printf (" initialize socket OK! \ R \ n "); // setting SOCKET port bzero (& s_add, sizeof (struct sockaddr_in); s_add.sin_family = af_inet; s_add.sin_addr.s_addr = htonl (inaddr_any ); s_add.sin_port = htons (portnum); // bind port if (-1 = BIND (SFP, (struct sockaddr *) (& s_add), sizeof (struct sockaddr ))) {printf ("bind fail! \ R \ n "); Return-1;} printf (" bind OK! \ R \ n "); // start listening to the client port if (-1 = listen (SFP, 5) {printf (" Listen fail! \ R \ n "); Return-1;} printf (" Listen OK \ r \ n "); While (1) {sin_size = sizeof (struct sockaddr_in ); // accept data from the client nfp = accept (SFP, (struct sockaddr *) (& c_add), & sin_size); If (-1 = NFP) {printf ("Accept fail! \ R \ n "); Return-1;} // printf (" Accept data from client OK! \ R \ nserver start get connect from % d: % x \ r \ n ", ntohl (c_add.sin_addr.s_addr), ntohs (c_add.sin_port )); printf ("current client is address = % d, Port: % x \ r \ n", ntohl (c_add.sin_addr.s_addr), s_add.sin_port ); // send data if (-1 = write (nfp, "server said, \" Hello, welcome to connect my server. \ "\ r \ n", 1024) {printf ("send data to client fail! \ R \ n "); Return-1;} printf (" send data to client OK! \ R \ n "); close (NFP);} Close (SFP); Return 0 ;}


Client code

C:

////  main.c//  unix_socket_very_simple_client////  Created by DMD on 4/7/14./* Function : Client for unix (OS X MAC)*/#include <stdlib.h>#include <sys/types.h>#include <stdio.h>#include <sys/socket.h>#include <netinet/in.h>#include <string.h>int main(){    int cfd;    int recbytes;    int sin_size;    char buffer[1024]={0};    struct sockaddr_in s_add,c_add;    unsigned short portnum=12345;    printf("Hello,welcome to client !\r\n");        cfd = socket(AF_INET, SOCK_STREAM, 0);    if(-1 == cfd)    {        printf("initialize socket fail ! \r\n");        return -1;    }    printf("initialize socket ok !\r\n");        bzero(&s_add,sizeof(struct sockaddr_in));    s_add.sin_family=AF_INET;    s_add.sin_addr.s_addr= inet_addr("127.0.0.1");    s_add.sin_port=htons(portnum);    printf("current server addr = %d ,port : %x\r\n",s_add.sin_addr.s_addr,s_add.sin_port);        if(-1 == connect(cfd,(struct sockaddr *)(&s_add), sizeof(struct sockaddr)))    {        printf("connect fail !\r\n");        return -1;    }    printf("connect ok !\r\n");        if(-1 == (recbytes = read(cfd,buffer,1024)))    {        printf("read data from server fail !\r\n");        return -1;    }    printf("read data from server ok\r\n");    printf("Get Data From Server:\r\n");    buffer[recbytes]='\0';    printf("%s\r\n",buffer);    getchar();    close(cfd);    return 0;}


Test:


Run: Server

Run: Client

1. Show some message from server in client.

2. Every open client, show some message in server


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.