#include <netinet/in.h> #include <string.h> #include <arpa/inet.h> # include <sys/types.h> #include <sys/types.h> #include <sys/socket.h> #include <unistd.h> #include <stdio.h> #include <stdlib.h> #define BIND_PORT 9999 #define BUFFSZ 4096 int main(int argc, char * argv[]) { //extern int socket (int __domain, int __type, int __protocol) //sockaddr_in 代表一个网络地址。这里定义一个socaddr_in的服务器端地址 struct sockaddr_in server_addr; //清零 bzero(&server_addr, sizeof(server_addr)); //服务器端使用的端口 server_addr.sin_port = htons(BIND_PORT); //服务器端使用的协议家族 server_addr.sin_family = AF_INET; //服务器端使用的地址,INADDR_ANY表示由系统自己分配本机的IP地址 server_addr.sin_addr.s_addr = htons(INADDR_ANY); //创建一个服务器的socket int server_socket = socket(PF_INET, SOCK_STREAM, 0); if ( server_socket < 0) { perror("Create server socket error!"); exit(1); } //创建成功后绑定 if ( bind(server_socket, (struct sockaddr *)&server_addr, sizeof(server_addr)) == -1 ) { perror("bind socket error!"); exit(1); } //绑定成功后开始监听 if ( listen(server_socket, 10) == -1) { perror("listen socket error!"); exit(1); } while(1) { //extern int accept (int __fd, __SOCKADDR_ARG __addr, socklen_t *__restrict __addr_len); //定义一个客户端的地址信息结构体 struct sockaddr_in client_addr; //清零 bzero(&client_addr, sizeof(client_addr)); //获取结构体的大小 socklen_t length = sizeof(client_addr); //accept 接受一个请求,成功后返回该socket的fd,并保存客户端的地址信息 int communicate_socket; if ( (communicate_socket = accept(server_socket, (struct sockaddr *)(&client_addr), &length)) < 0 ) { perror("accept error!"); exit(1); } char msg[BUFFSZ]; bzero(msg, BUFFSZ); sprintf(msg, "Hello, I'm Server on Yangzhiyong-Linux\n"); //发送信息到客户端 write(communicate_socket, msg, sizeof(msg)); //干完事就关闭该次的连接 close(communicate_socket); } return EXIT_SUCCESS; }
Client:
#include <netinet/in.h> #include <string.h> #include <arpa/inet.h> # include <sys/types.h> #include <sys/types.h> #include <sys/socket.h> #include <unistd.h> #include <stdio.h> #include <stdlib.h> #define BIND_PORT 9999 #define BUFFSZ 4096 #define SERVER_IP "127.0.0.1" int main(int argc, char * argv[]) { //extern int socket (int __domain, int __type, int __protocol) struct sockaddr_in server_addr; struct sockaddr_in communite_addr; bzero(&server_addr, sizeof(server_addr)); bzero(&communite_addr, sizeof(communite_addr)); server_addr.sin_port = htons(BIND_PORT); server_addr.sin_family = AF_INET; server_addr.sin_addr.s_addr = inet_addr(SERVER_IP); //创建一个用于通信的socket int communicate_socket = socket(PF_INET, SOCK_STREAM, 0); if (communicate_socket < 0) { perror("Create communicate_socket error!"); exit(1); } //extern int connect (int __fd, __CONST_SOCKADDR_ARG __addr, socklen_t __len) //连接到服务器,第一个参数是一个socket对象,用于该次的通信通道,第二个参数是服务器的地址信息,第三个参数是该地址信息的大小 if (connect(communicate_socket, (struct sockaddr *)(&server_addr), sizeof(server_addr)) == -1) { perror("connect to server error!"); exit(1); } int readLen = 0; char revMsg[BUFFSZ]; bzero(revMsg, BUFFSZ); //读取服务器发过来的信息 if ((readLen = read(communicate_socket, revMsg, BUFFSZ)) == -1) { perror("read msg error!"); exit(1); } revMsg[readLen] = '\0'; printf("%s\n", revMsg); close(communicate_socket); return EXIT_SUCCESS; }