- server.c
- #include <sys/types.h>
- #include <sys/socket.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <arpa/inet.h>
- #include <stdio.h>
- #include <unistd.h>
- int main(int argc, char** argv)
- {
- int ret;
- //创建socket文件
- int sockfd = socket(AF_INET, SOCK_STREAM, 0);
- if(sockfd == -1)
- {
- perror("socket\n");
- exit(1);
- }
- printf("socket_fd = %d\n",sockfd);
- //重复使用本地址与socket文件进行绑定,如果不设置系统,会保留此连接直到最后一引用才释放,即进程结束后系统需要几分钟后才能重新进行绑定
- int i = 1;
- ret = setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &i, sizeof(i));
- if(ret)
- {
- perror("setsockopt\n");
- exit(1);
- }
- printf("setsockopt complete!\n");
- //创建服务器socket地址sock_addr
- struct sockaddr_in server_addr;
- server_addr.sin_family = AF_INET;
- server_addr.sin_port = htons(4444);
- //地址指定INADDR_ANY表示可以接收安装在系统里面所有网卡的数据包
- server_addr.sin_addr.s_addr = INADDR_ANY;
- memset(server_addr.sin_zero, 8, 8);
- //将socket与地址进行绑定,这样可以使所有发向端口4444及协议为IPV4数据包指向socket文件
- ret = bind(sockfd, (struct sockaddr*)&server_addr, sizeof(struct sockaddr));
- if(ret)
- {
- perror("bind\n");
- exit(1);
- }
- printf("bind complete!\n");
- //监听连接到该socket上请求,不会发生阻塞行为,以下设置队列大小为1,即队列满的时候其它连接会被拒绝连接
- ret = listen(sockfd, 10);
- if(ret)
- {
- perror("listen\n");
- exit(1);
- }
- printf("listen complete!\n");
- //接收连接请求并建立连接, 如果没有连接请求 accept会一址阻塞,直到有请求来
- struct sockaddr_in client_addr;
- int client_fd;
- socklen_t len = sizeof(struct sockaddr);
- client_fd = accept(sockfd, (struct sockaddr*)&client_addr, &len);
- if(client_fd == -1)
- {
- perror("bind\n");
- exit(1);
- }
- printf("accept complete!\n");
- //请取client发来的信息
- char buf[100];
- ssize_t readnum;
- memset(buf,100,0);
- //这里应该是客户端的socket文件描述符
- readnum = recv(client_fd, (void*)buf, 100, MSG_WAITALL);
- //清空标准输出
- fflush(stdout);
- printf("received:%s\nnumber byte:%d\n",buf,strlen(buf));
- //发送信息到用户端
- ssize_t sendnum;
- char content[100]="\"server:received the data!\"";
- sendsendnum = send(client_fd, content, 100, 0);
- printf("reply:%s\nnumber byte:%d\n",content,sendnum);
- // 关闭socket文件
- close(sockfd);
- close(client_fd);
- return 0;
- }
- client.c
- #include <sys/types.h>
- #include <sys/socket.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <arpa/inet.h>
- #include <stdio.h>
- #include <unistd.h>
- #include <arpa/inet.h>
- int main(int argc, char** argv)
- {
- int ret;
- //创建socket文件
- int sockfd = socket(AF_INET, SOCK_STREAM, 0);
- if(sockfd == -1)
- {
- perror("socket\n");
- exit(1);
- }
- printf("socket_fd = %d\n",sockfd);
- //创建服务器socket地址sock_addr
- struct sockaddr_in server_addr;
- server_addr.sin_family = AF_INET;
- server_addr.sin_port = htons(4444);
- //将字符串地址转换成网络地址,有两种方法
- inet_pton(AF_INET, "127.0.0.1", (void*)&server_addr.sin_addr.s_addr);//IPV4 IPV6可用
- //server_addr.sin_addr.s_addr = inet_addr("127.0.0.1");//只能转IPV4
- memset(server_addr.sin_zero, 8, 0);
- //连接服务端
- ret = connect(sockfd, (struct sockaddr*)&server_addr,sizeof(struct sockaddr));
- if(ret == -1)
- {
- perror("connect\n");
- exit(1);
- }
- printf("connect complete!\n");
- //发送信息
- char content[100] = "\"hello,i am client!\"";
- ssize_t sendsendnum = send(sockfd, content, strlen(content)+1, 0);
- printf("send complete\nnumber byte:%d\ncontent:%s\n",sendnum,content);
- //收信息
- char buf[100];
- ssize_t readnum;
- memset(buf,100,0);
- readnum = recv(sockfd, (void*)buf, 100, MSG_WAITALL);
- //清空标准输出
- fflush(stdout);
- printf("received:%s\nnumber byte:%d\n",buf,strlen(buf));
- //关闭socket
- close(sockfd);
- return 0;
- }
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。