赞
踩
步骤如图所示
1 创建Socket 指定使用什么协议
socket_fd = socket(AF_INET,SOCK_STREAM,0);
2 将Socket与地址和端口绑定
//set socket address
local_addr.sin_family = AF_INET;
local_addr.sin_port = htons(PORT);
local_addr.sin_addr.s_addr = INADDR_ANY;
bzero(&(local_addr.sin_zero),8);
//bind socket
ret = bind(socket_fd,(struct sockaddr*)&local_addr,sizeof(struct sockaddr_in));
3 侦听端口(目前Socket和地址 端口绑定了 但是上层应用必须绑定对应的Socket)
ret = listen(socket_fd,backlog);
4 创建新的socket – 每次连接过来以后创建一个新的socket来收发数据 也就是accept socket (之前的socket 一直在侦听 请求连接是数据)
int addr_len = sizeof( struct sockaddr_in );
//accept an new connection
accept_fd = accept( socket_fd, (struct sockaddr *)&remote_addr, &addr_len );
5 recv接收数据
ret = recv( accept_fd ,(void*)in_buf ,MESSAGE_SIZE ,0 );
6 send发送数据
send(accept_fd, (void*)in_buf, MESSAGE_SIZE, 0);
7 close关闭当前socket
close(accept_fd);
close(socket_fd);
// // Created by zyc on 2022/5/5. // #include <stdio.h> #include <unistd.h> #include <stdlib.h> #include <string.h> #include <sys/socket.h> #include <netinet/in.h> #define MESSAGE_SIZE 1024 #define PORT 8111 int main(){ int ret = 0; int socket_fd = -1; int accept_fd = -1; int curpos = 0; int backlog = 10; int flag = 1; char in_buf[MESSAGE_SIZE] = {0,}; struct sockaddr_in local_addr, remote_addr; //create a tcp socket socket_fd = socket(AF_INET,SOCK_STREAM,0); if (socket_fd==-1){ perror("create socket error"); exit(1); } //set option of socket ret = setsockopt(socket_fd,SOL_SOCKET,SO_REUSEADDR, &flag,sizeof(flag)); if ( ret == -1 ){ perror("setsockopt error"); } //set socket address local_addr.sin_family = AF_INET; local_addr.sin_port = htons(PORT); local_addr.sin_addr.s_addr = INADDR_ANY; bzero(&(local_addr.sin_zero),8); //bind socket ret = bind(socket_fd,(struct sockaddr*)&local_addr,sizeof(struct sockaddr_in)); if(ret == -1 ) { perror("bind error"); exit(1); } ret = listen(socket_fd,backlog); if ( ret == -1 ){ perror("listen error"); exit(1); } //loop for(;;){ int addr_len = sizeof( struct sockaddr_in ); //accept an new connection accept_fd = accept( socket_fd, (struct sockaddr *)&remote_addr, &addr_len ); for(;;){ memset(in_buf, 0, MESSAGE_SIZE); //receive network data and print it ret = recv( accept_fd ,(void*)in_buf ,MESSAGE_SIZE ,0 ); if(ret == 0 ){ break; } printf( "receive message:%s\n", in_buf ); send(accept_fd, (void*)in_buf, MESSAGE_SIZE, 0); } printf("close client connection...\n"); close(accept_fd); } printf("quit server...\n"); close(socket_fd); return 0; }
客户端 类型 但是少了 绑定和侦听
因为在Connect阶段系统会默认分配
但是需要提前输入服务端的地址 代码如下
serverAddr.sin_family = AF_INET;
serverAddr.sin_port = htons(SERVER_PORT);
//inet_addr()函数,将点分十进制IP转换成网络字节序IP
serverAddr.sin_addr.s_addr = inet_addr("127.0.0.1");
if(connect(socket_fd, (struct sockaddr *)&serverAddr, sizeof(serverAddr)) < 0)
{
perror("connect");
return 1;
}
printf("success to connect server...\n");
// // Created by zyc on 2022/5/5. // #include <stdio.h> #include <string.h> #include <stdlib.h> #include <unistd.h> #include <fcntl.h> #include <errno.h> #include <netdb.h> #include <sys/stat.h> #include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> #include <arpa/inet.h> #define SERVER_PORT 8111 #define MESSAGE_LENGTH 1024 int main(){ int ret = -1; int socket_fd; //server addr struct sockaddr_in serverAddr; char sendbuf[MESSAGE_LENGTH]; char recvbuf[MESSAGE_LENGTH]; int data_len; if((socket_fd = socket(AF_INET, SOCK_STREAM, 0)) < 0) { perror("socket"); return 1; } serverAddr.sin_family = AF_INET; serverAddr.sin_port = htons(SERVER_PORT); //inet_addr()函数,将点分十进制IP转换成网络字节序IP // serverAddr.sin_addr.s_addr = inet_addr("39.105.185.198"); serverAddr.sin_addr.s_addr = inet_addr("127.0.0.1"); if(connect(socket_fd, (struct sockaddr *)&serverAddr, sizeof(serverAddr)) < 0) { perror("connect"); return 1; } printf("success to connect server...\n"); while(1) { memset(sendbuf, 0, MESSAGE_LENGTH); printf("<<<<send message:"); gets(sendbuf); //printf("\n"); ret = send(socket_fd, sendbuf, strlen(sendbuf), 0); if(ret <= 0 ){ printf("the connection is disconnection!\n"); break; } if(strcmp(sendbuf, "quit") == 0){ break; } printf(">>> echo message:"); recvbuf[0] = '\0'; data_len = recv(socket_fd, recvbuf, MESSAGE_LENGTH, 0); recvbuf[data_len] = '\0'; printf("%s\n", recvbuf); } close(socket_fd); return 0; }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。