C语言Socket编程基础
Socket编程是一种网络编程的方法,它允许在同一台或不同的计算机之间进行通信,在C语言中,我们可以使用socket API来实现这种通信,本文将介绍C语言socket编程的基础知识,包括socket的定义、创建、绑定、监听、连接、数据传输等步骤。
1、Socket定义
在计算机网络中,socket是一种抽象的概念,它代表了一个网络连接的端点,每个socket都有一个唯一的IP地址和端口号,用于标识一个特定的网络连接,在C语言中,我们使用结构体sockaddr_in来表示一个socket。
2、创建socket
在C语言中,我们使用socket()函数来创建一个socket,这个函数需要一个参数,即协议类型,常用的协议类型有IPv4(AF_INET)和IPv6(AF_INET6),创建一个IPv4类型的socket,可以使用以下代码:
#include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> #include <arpa/inet.h> #include <unistd.h> int main() { int sockfd; struct sockaddr_in servaddr; sockfd = socket(AF_INET, SOCK_STREAM, 0); if (sockfd < 0) { perror("socket"); exit(1); } return 0; }
3、绑定socket
在创建了socket之后,我们需要将其绑定到一个特定的IP地址和端口号上,这可以通过bind()函数来实现,将上述创建的socket绑定到本地IP地址127.0.0.1的8080端口上,可以使用以下代码:
#include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> #include <arpa/inet.h> #include <unistd.h> #include <string.h> #include <stdio.h> int main() { int sockfd; struct sockaddr_in servaddr; const char *ip = "127.0.0.1"; const int port = 8080; sockfd = socket(AF_INET, SOCK_STREAM, 0); if (sockfd < 0) { perror("socket"); exit(1); } memset(&servaddr, 0, sizeof(servaddr)); servaddr.sin_family = AF_INET; servaddr.sin_port = htons(port); inet_pton(AF_INET, ip, &servaddr.sin_addr); if (bind(sockfd, (struct sockaddr *)&servaddr, sizeof(servaddr)) < 0) { perror("bind"); exit(1); } return 0; }
4、监听socket
在绑定了socket之后,我们需要监听这个socket上的连接请求,这可以通过listen()函数来实现,将上述绑定的socket设置为监听状态,可以设置最大连接数为5,可以使用以下代码:
#include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> #include <arpa/inet.h> #include <unistd.h> #include <string.h> #include <stdio.h> #include <stdlib.h> #include <errno.h> #include <fcntl.h> #include <sys/stat.h> #include <sys/wait.h> #include <signal.h> #include <sys/select.h> #include <sys/time.h> #include <sys/resource.h> #include <sys/utsname.h> #include <netdb.h> #include <sys/un.h> #include <sys/epoll.h> #include <poll.h> #include <termios.h> #include <unistd.h> #include <fcntl.h> #include <sys/types.h> #include <sys/stat.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <errno.h> #include <assert.h> #include <syslog.h> // for syslog() function to log messages to the system log (not console) or a file (depending on configuration) at different priority levels (debug, info, notice, warning, error, critical) and facility level (local0-local7). The priority level is specified by the first parameter of the syslog() function and the facility level by the second parameter (both are optional). The format of the message is specified by the third parameter of the syslog() function (optional). If not specified, it defaults to the format used by the syslog() function when called with the same priority and facility level as this call but without any additional parameters (i.e., just the message string). The return value of the syslog() function is zero if successful; otherwise, it returns -1 and sets errno to indicate the error that occurred (e.g., EACCES if the process does not have permission to write to the system log or a file at the specified priority and facility level). Note that the syslog() function is thread-safe and can be called from multiple threads simultaneously without causing any race conditions or other issues as long as each thread uses a separate file descriptor for logging (i
还没有评论,来说两句吧...