在进行socket网络编程时, 我们需要了解一些必备的知识,例如什么是socket,ipv4的地址结构,套接字类型等等,不然上来直接看代码就会晕,当初学习网络编程时,看书上的例子,总有感觉书上讲的都很简要。再或者讲的原理太多把人绕晕。我这里只想让大家简单知道怎么使用socket进行网络编程并且给出的例子可以直接使用参考。
1. 什么是socket
#include<sys/socket.h> #include<sys/types.h> #include<unistd.h> #include<stdlib.h> #include<netinet/in.h> #include<arpa/inet.h> #include<stdio.h> #include<errno.h> #include<string.h> #define ERR_EXIT(m)\ do \ { \ perror(m); \ exit(EXIT_FAILURE); \ } while(0) int main() { int listenfd; listenfd = socket(PF_INET, SOCK_STREAM, 0); if(listenfd < 0) ERR_EXIT("socket"); struct sockaddr_in seraddr; memset(&seraddr, 0, sizeof(seraddr)); seraddr.sin_family = AF_INET; seraddr.sin_port = htons(5788); seraddr.sin_addr.s_addr = htonl(INADDR_ANY); int bindval = bind(listenfd, (struct sockaddr*)&seraddr, sizeof(seraddr); if(bindval < 0) ERR_EXIT("bind"); if((listen(listenfd, SOMAXCONN)) < 0) ERR_EXIT("listen"); struct sockaddr_in peeraddr; socklen_t peerlen = sizeof(peeraddr); int conn = accept(listenfd, (struct sockaddr*)&peeraddr, &peerlen); if(conn < 0) ERR_EXIT("accept"); char recvbuf[1024]; while(1) { memset(recvbuf, 0, sizeof(recvbuf)); int recvlen = read(conn, recvbuf, sizeof(recvbuf)); fputs(recvbuf, stdout); write(conn, recvbuf, recvlen); } close(listenfd); return 0; }
#include<sys/socket.h> #include<sys/types.h> #include<unistd.h> #include<stdlib.h> #include<netinet/in.h> #include<arpa/inet.h> #include<stdio.h> #include<errno.h> #include<string.h> #define ERR_EXIT(m)\ do \ { \ perror(m); \ exit(EXIT_FAILURE); \ } while(0) int main() { int sockfd; sockfd = socket(PF_INET, SOCK_STREAM, 0); if(sockfd < 0) ERR_EXIT("socket"); struct sockaddr_in seraddr; memset(&seraddr, 0, sizeof(seraddr)); seraddr.sin_family = AF_INET; seraddr.sin_port = htons(5788); seraddr.sin_addr.s_addr = inet_addr("127.0.0.1"); int conn = connect(sockfd, (struct sockaddr*)&seraddr, sizeof(seraddr)); if(conn < 0) ERR_EXIT("connect"); char recvbuf[1024]; char sendbuf[1024]; while(fgets(sendbuf, sizeof(sendbuf), stdin) != NULL) { write(sockfd, sendbuf, sizeof(sendbuf)); read(sockfd, recvbuf, sizeof(recvbuf)); fputs(recvbuf, stdout); memset(sendbuf, 0, sizeof(sendbuf)); memset(recvbuf, 0, sizeof(recvbuf)); } close(sockfd); return 0; }
int on = 1; //等于1代表开启 if(setsockopt(listenfd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) < 0) ERR_EXIT("setsockopt");
#include<sys/socket.h> #include<sys/types.h> #include<unistd.h> #include<stdlib.h> #include<netinet/in.h> #include<arpa/inet.h> #include<stdio.h> #include<errno.h> #include<string.h> #define ERR_EXIT(m)\ do \ { \ perror(m); \ exit(EXIT_FAILURE); \ } while(0) void do_service(int conn) { char recvbuf[1024]; while(1) { memset(recvbuf, 0, sizeof(recvbuf)); int recvlen = read(conn, recvbuf, sizeof(recvbuf)); if(recvlen == 0) { printf("clent close\n"); break; } if(recvlen == -1) { printf("read data error\n"); break; } fputs(recvbuf, stdout); write(conn, recvbuf, recvlen); } } int main() { int listenfd; listenfd = socket(PF_INET, SOCK_STREAM, 0); if(listenfd < 0) ERR_EXIT("socket"); struct sockaddr_in seraddr; memset(&seraddr, 0, sizeof(seraddr)); seraddr.sin_family = AF_INET; seraddr.sin_port = htons(5788); seraddr.sin_addr.s_addr = htonl(INADDR_ANY); int on = 1; if(setsockopt(listenfd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) < 0) ERR_EXIT("setsockopt"); int bindval = bind(listenfd, (struct sockaddr*)&seraddr, sizeof(seraddr) if(bindval < 0) ERR_EXIT("bind"); if((listen(listenfd, SOMAXCONN)) < 0) ERR_EXIT("listen"); struct sockaddr_in peeraddr; socklen_t peerlen = sizeof(peeraddr); pid_t pid; while(1) { int conn = accept(listenfd, (struct sockaddr*)&peeraddr, &peerlen); if(conn < 0) ERR_EXIT("accept"); printf("address:%s, port:%d\n",inet_ntoa(peeraddr.sin_addr), ntohs(pe pid = fork(); if (pid == -1) ERR_EXIT("fork"); if (pid == 0) { close (listenfd); do_service (conn); exit (EXIT_SUCCESS); //client close,so close subprocess //if not closed, it is will continue //recv connect. } else { close(conn); } } close(listenfd); return 0; }