码迷,mamicode.com
首页 > 其他好文 > 详细

MAC OS X socket 1 [a simple example]

时间:2014-07-06 11:33:27      阅读:164      评论:0      收藏:0      [点我收藏+]

标签:blog   http   使用   数据   os   2014   

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>

//服务器和客户端都使用端口:12345
//服务器的地址是:127.0.0.1 在服务器里可以写任意地址,但是在客户端必须写这个地址。
//因为不知道服务器地址,就无法向服务器发送数据
int main(int argc, const char * argv[])
{
    // listen port [客户端必须使用这个端口]
    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");
    
    //开始监听客户端端口
    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);
        //接受来自客户端的数据
        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);
        
        
        //向客户端发送数据
        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:

bubuko.com,布布扣

Run : Server

Run :Client

1.Show some message from server in Client.

2.every open Client, show some message in Server


MAC OS X socket 1 [a simple example],布布扣,bubuko.com

MAC OS X socket 1 [a simple example]

标签:blog   http   使用   数据   os   2014   

原文地址:http://blog.csdn.net/u013317006/article/details/36868597

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!