您好,登錄后才能下訂單哦!
- 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進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。