参考网上很多讲的很清晰地关于socket应用的理论,所以写了几个例子进行练习。
1,socket-bind-connect-读取数据-close 本机内客户端和服务器进行通信测试。
服务器:
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <sys/types.h>
#include<netinet/in.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <string.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#define LISTEN_BACKLOG 10
#define SERVER_PORT 6666
#define MAXLINE 4096
#define handle_error(msg) \
do { perror(msg); exit(EXIT_FAILURE); } while (0)
int
main(int argc, char *argv[])
{
int sfd, cfd;
struct sockaddr_in serverAddr, clientAddr;
char sendbuf[200];
char rcvbuf[200];
int iDataNum;
socklen_t clientAddrlen = sizeof(clientAddr);
sfd = socket(AF_INET, SOCK_STREAM, 0);
if (sfd == -1)
{
printf("socket");
return 1;
}
memset(&serverAddr, 0,