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 //Sever int main(int argc, const char * argv[]) { printf("\nServer:\n"); ///定义sockfd int 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"); ///定义sockaddr_in struct sockaddr_in server_sockaddr; server_sockaddr.sin_family = AF_INET; server_sockaddr.sin_port = htons(MYPORT); server_sockaddr.sin_addr.s_addr = htonl(INADDR_ANY); ///bind,成功返回0,出错返回-1 if(bind(server_sockfd,(struct sockaddr *)&server_sockaddr,sizeof(server_sockaddr))==-1) { perror("bind"); exit(1); } printf("bind ok !\r\n"); ///listen,成功返回0,出错返回-1 if(listen(server_sockfd,QUEUE) == -1) { perror("listen"); exit(1); } printf("Starting listen......\r\n"); printf("(Please run Client and input something)\r\n"); ///客户端套接字 char buffer[BUFFER_SIZE]; char send_client_buffer[BUFFER_SIZE]; struct sockaddr_in client_addr; socklen_t length = sizeof(client_addr); ///成功返回非负描述字,出错返回-1 int conn = accept(server_sockfd, (struct sockaddr*)&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)); //开始接受来自服务器信息 int len = recv(conn, buffer, sizeof(buffer),0); //printf("Client Said,\"%s\"\n",buffer); printf("client said:%s",buffer); //如果输入"exit",表示让服务器跳出while,即不再接受来自客户端的数据,并且关闭服务器 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); //将信息输出到屏幕上 //fputs(send_client_buffer, stdout); } //将字符串buffer发送给客户端conn里。 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(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 //在main函数里面加入参数,会让第一次输入的时候得到一个未知字符。 //int main(int argc, const char * argv[]) int main() { printf("\nClient:\n"); ///定义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"); ///定义sockaddr_in struct sockaddr_in servaddr; memset(&servaddr, 0, sizeof(servaddr)); servaddr.sin_family = AF_INET; servaddr.sin_port = htons(MYPORT); ///服务器端口 servaddr.sin_addr.s_addr = inet_addr("127.0.0.1"); ///服务器ip ///连接服务器,成功返回0,错误返回-1 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)); //客户端:输入字符串到sendbuf,用回车结束 while (fgets(sendbuf, sizeof(sendbuf), stdin) != NULL) { //将数据发送到服务器端 send(sock_cli, sendbuf, strlen(sendbuf),0); ///发送 //如果输入的是exit,就退出 if(strcmp(sendbuf,"exit\n")==0) { break; } //接受来自服务器的反馈信息 recv(sock_cli, recvbuf, sizeof(recvbuf),0); ///接收 //将信息输出到屏幕上 //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!
原理图:服务器和客户端通信
END
2014-7-3 by DMD
MAC OS X socket 2 [a simple example],布布扣,bubuko.com
MAC OS X socket 2 [a simple example]
原文地址:http://blog.csdn.net/u013317006/article/details/36887417