客户端 服务器端

server:

#import <Foundation/Foundation.h>

#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <unistd.h>
#include <sys/socket.h>
#include <netinet/in.h>
#import <arpa/inet.h>
#include <sys/types.h>
#include <netdb.h>


int main(int argc, const char * argv[]) {
    @autoreleasepool {
        // insert code here...
        NSLog(@"Hello, World!");
        
        
        
        int sockfd,connfd;
        struct sockaddr_in srvaddr;
        struct sockaddr_in cliaddr;
        int len,port;
        char hello[]="Hi,welcome to linux-code!\n";
        if((sockfd=socket(AF_INET,SOCK_STREAM,0))==-1){
            fprintf(stderr,"Socket error:%s\n\a",strerror(errno));
            exit(1);
        }
        /* 服务器端填充 sockaddr结构 */
        bzero(&srvaddr,sizeof(struct sockaddr_in));
        srvaddr.sin_family=AF_INET;
//        srvaddr.sin_addr.s_addr=htonl(INADDR_ANY);//INADDR_ANY就是指定地址为0.0.0.0的地址,这个地址事实上表示不确定地址,或“所有地址”、“任意地址”。 一般来说,在各个系统中均定义成为0值。
        
        
        if (inet_pton(AF_INET, "192.168.1.5", &srvaddr.sin_addr) <= 0)  //ok
        {
            
            fprintf(stderr,"inet_pton Error:%s\a\n",strerror(errno));
            exit(1);
        }
//        srvaddr.sin_addr.s_addr=htonl("192.168.1.5"); //error
        
        srvaddr.sin_port=htons(1113);
        /* 捆绑sockfd描述符 */
        if(bind(sockfd,(struct sockaddr *)(&srvaddr),sizeof(struct sockaddr))==-1){
            fprintf(stderr,"Bind error:%s\n\a",strerror(errno));
            exit(1);
        }
        /* 监听sockfd描述符 */
        if(listen(sockfd,5)==-1){
            fprintf(stderr,"Listen error:%s\n\a",strerror(errno));
            exit(1);
        }
        len=sizeof(struct sockaddr_in);
        while(1){ /* 服务器阻塞,直到客户程序建立连接 */
            if((connfd=accept(sockfd,(struct sockaddr *)(&cliaddr),&len))==-1){
                fprintf(stderr,"Accept error:%s\n\a",strerror(errno));
                exit(1);
            }
            fprintf(stderr,"Server get connection from %s\n",inet_ntoa(cliaddr.sin_addr));
            if(write(connfd,hello,strlen(hello))==-1){
                fprintf(stderr,"Write Error:%s\n",strerror(errno));
                exit(1);
            }
            /* 这个通讯已经结束 */
            /* 循环下一个 */
        }
        close(sockfd);
        exit(0);
    }
    return 0;
}

 客户端:

#import <Foundation/Foundation.h>


#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <unistd.h>
#include <sys/socket.h>
#include <netinet/in.h>
#import <arpa/inet.h>
#include <sys/types.h>
#include <netdb.h>


int main(int argc, const char * argv[]) {
    @autoreleasepool {
        // insert code here...
        NSLog(@"Hello, client!");
        
        int sockfd; char buf[1024];
        struct sockaddr_in srvaddr;
        struct hostent *phost; int nbytes;
        
//        if(argc!=3){
//            fprintf(stderr,"Usage:%s <IP> <portnumber>\a\n",argv[0]);
//            exit(1);
//        }
        
        /* 客户程序开始建立 sockfd描述符 */
        if((sockfd=socket(AF_INET,SOCK_STREAM,0))==-1){
            fprintf(stderr,"socket Error:%s\a\n",strerror(errno));
            exit(1);
        }
        /* 客户程序填充服务端的资料 */
        bzero(&srvaddr,sizeof(srvaddr));
        
        srvaddr.sin_family=AF_INET;
        
//        srvaddr.sin_port=htons(atoi(argv[2]));
//        if (inet_pton(AF_INET, argv[1], &srvaddr.sin_addr) <= 0){
//            fprintf(stderr,"inet_pton Error:%s\a\n",strerror(errno));
//            exit(1);
//        }
        
        
        srvaddr.sin_port=htons(atoi("1113"));
        if (inet_pton(AF_INET, "192.168.1.5", &srvaddr.sin_addr) <= 0){
            
            fprintf(stderr,"inet_pton Error:%s\a\n",strerror(errno));
            exit(1);
        }
        
        /* 客户程序发起连接请求 */
        if(connect(sockfd,(struct sockaddr *)(&srvaddr),sizeof(struct sockaddr))==-1){
            fprintf(stderr,"connect Error:%s\a\n",strerror(errno));
            exit(1);
        }
        /* 连接成功了 */
        if((nbytes=read(sockfd,buf,1024))==-1){
            fprintf(stderr,"read Error:%s\n",strerror(errno));
            exit(1);
        }
        buf[nbytes]='\0';
        printf("received data:%s\n",buf);
        /* 结束通讯 */
        close(sockfd);
        exit(0);
        
        
    }
    return 0;
}

 

posted @ 2014-11-17 16:56  阿哲工具箱diqqxu  阅读(156)  评论(0)    收藏  举报