1、tcp_client.c
#include <stdio.h>
#include <sys/types.h> /* See NOTES */
#include <sys/socket.h>
#include <arpa/inet.h>
#include <string.h>
#include <unistd.h>
#include <pthread.h>
#include <netinet/in.h>
// 子线程负责写数据
void *write_data(void *v)
{
int client_socket = *(int*)v;
char buf[1024];
while (1)
{
fgets(buf, 1024, stdin);
write(client_socket, buf, strlen(buf));
}
}
// 主线程负责读数据
void read_data(int client_socket)
{
char buf[1024] = {"asdsadsadsadsa"};
while (1)
{
int ret = read(client_socket, buf, sizeof(buf)-1);
if (-1 == ret)
{
perror ("read error");
}
if (0 == ret)
{
printf ("客户端退出\n");
break;
}
buf[ret] = '\0';
printf ("%s\n", buf);
}
}
int main(int argc, char **argv)
{
// 1、创建套接字
int conn_socket = socket(AF_INET, SOCK_STREAM, 0);
if(-1 == conn_socket)
{
perror("创建套接字失败");
return -1;
}
// 2、连接服务器
struct sockaddr_in addr;
memset(&addr, 0, sizeof(struct sockaddr_in));
addr.sin_family = AF_INET; /* Internet地址族 */
addr.sin_port = htons(atoi(argv[2])); /*服务器用的端口号 */
addr.sin_addr.s_addr = inet_addr(argv[1]); /*服务器用的IP地址 */
int ret = connect(conn_socket, (const struct sockaddr *)&addr, sizeof(addr));
if (-1 == ret)
{
perror("连接服务器失败");
return -1;
}
pthread_t thread;
pthread_create(&thread, NULL, write_data, (void *)&conn_socket);
read_data(conn_socket);
close(conn_socket);
return 0;
}
2、多进程并发网络编程
#include <stdio.h>
#include <sys/types.h> /* See NOTES */
#include <sys/socket.h>
#include <arpa/inet.h>
#include <string.h>
#include <unistd.h>
#define PORT 9998
void handl_client(int client_socket)
{
char buf[1024] = {"asdsadsadsadsa"};
while (1)
{
int ret = read(client_socket, buf, sizeof(buf)-1);
if (-1 == ret)
{
perror ("read error");
break;
}
if (0 == ret)
{
printf ("客户端退出\n");
break;
}
buf[ret] = '\0';
printf ("收到客户端数据:%s\n", buf);
int i;
for (i = 0; i < ret-1; i++)
{
buf[i] += 'A' - 'a';
}
write(client_socket, buf, ret);
}
}
// 监听套接字
int init()
{
int listen_socket = socket(AF_INET, SOCK_STREAM, 0);
if(-1 == listen_socket)
{
perror("创建套接字失败");
return -1;
}
struct sockaddr_in addr;
memset(&addr, 0, sizeof(struct sockaddr_in));
addr.sin_family = AF_INET; /* Internet地址族 */
addr.sin_port = htons(PORT); /* 端口号 */
addr.sin_addr.s_addr = htonl(INADDR_ANY); /* IP地址, 绑定本地的所有ip地址*/
int ret = bind(listen_socket, (const struct sockaddr *)&addr, sizeof(addr));
if(-1 == ret)
{
perror("绑定失败");
return -1;
}
// 3、监听套接字
ret = listen(listen_socket, 5);
if(-1 == ret)
{
perror("监听失败");
return -1;
}
return listen_socket;
}
// 通信套接字
int myAccept(int listen_socket)
{
struct sockaddr_in client_addr;
socklen_t len = sizeof(client_addr);
int client_socket = accept(listen_socket, (struct sockaddr *)&client_addr, &len);
if (-1 == client_socket)
{
perror("accept 失败");
return -1;
}
printf ("客户端的 ip = %s, 端口 = %d\n", inet_ntoa(client_addr.sin_addr), ntohs(client_addr.sin_port));
return client_socket;
}
int main(int argc, char **argv)
{
int listen_socket = init();
if (-1 == listen_socket)
return;
while (1)
{
int client_socket = myAccept(listen_socket);
pid_t pid = fork() ;// 创建子进程为客户端服务
switch(pid)
{
case -1:
perror("fork失败");
break;
case 0: // 子进程
close(listen_socket); // 关闭监听套接字
handl_client(client_socket); // 处理客户端请求
close(client_socket); // 关闭通信套接字
break;
default: // 父进程
close(client_socket); // 关闭通信套接字
break;
}
}
close(listen_socket);
return 0;
}
3、多线程并发网络编程
#include <stdio.h>
#include <sys/types.h> /* See NOTES */
#include <sys/socket.h>
#include <arpa/inet.h>
#include <string.h>
#include <unistd.h>
#include <pthread.h>
#define PORT 9998
void *handl_client(void *v)
{
long client_socket = (long)v;
char buf[1024] = {"asdsadsadsadsa"};
while (1)
{
int ret = read(client_socket, buf, sizeof(buf)-1);
if (-1 == ret)
{
perror ("read error");
break;
}
if (0 == ret)
{
printf ("客户端退出\n");
break;
}
buf[ret] = '\0';
printf ("收到客户端数据:%s\n", buf);
int i;
for (i = 0; i < ret-1; i++)
{
buf[i] += 'A' - 'a';
}
write(client_socket, buf, ret);
}
}
// 监听套接字
int init()
{
int listen_socket = socket(AF_INET, SOCK_STREAM, 0);
if(-1 == listen_socket)
{
perror("创建套接字失败");
return -1;
}
struct sockaddr_in addr;
memset(&addr, 0, sizeof(struct sockaddr_in));
addr.sin_family = AF_INET; /* Internet地址族 */
addr.sin_port = htons(PORT); /* 端口号 */
addr.sin_addr.s_addr = htonl(INADDR_ANY); /* IP地址, 绑定本地的所有ip地址*/
int ret = bind(listen_socket, (const struct sockaddr *)&addr, sizeof(addr));
if(-1 == ret)
{
perror("绑定失败");
return -1;
}
// 3、监听套接字
ret = listen(listen_socket, 5);
if(-1 == ret)
{
perror("监听失败");
return -1;
}
return listen_socket;
}
// 通信套接字
int myAccept(int listen_socket)
{
struct sockaddr_in client_addr;
socklen_t len = sizeof(client_addr);
int client_socket = accept(listen_socket, (struct sockaddr *)&client_addr, &len);
if (-1 == client_socket)
{
perror("accept 失败");
return -1;
}
printf ("客户端的 ip = %s, 端口 = %d\n", inet_ntoa(client_addr.sin_addr), ntohs(client_addr.sin_port));
return client_socket;
}
int main(int argc, char **argv)
{
int listen_socket = init();
if (-1 == listen_socket)
return;
while (1)
{
long client_socket = myAccept(listen_socket);
if (-1 == client_socket)
{
continue;
}
// 开辟线程为客户端服务
pthread_t thread;
pthread_create(&thread, NULL, handl_client, (void *)client_socket);
pthread_detach(thread); // 线程分离
}
close(listen_socket);
return 0;
}
4、简单聊天
#include <stdio.h>
#include <sys/types.h> /* See NOTES */
#include <sys/socket.h>
#include <arpa/inet.h>
#include <string.h>
#include <unistd.h>
#include <pthread.h>
#define PORT 9997
int socket_fd[2];
void *handl_client(void *v)
{
long i = (long)v;
char buf[1024] = {"asdsadsadsadsa"};
while (1)
{
int ret = read(socket_fd[i], buf, sizeof(buf)-1);
if (-1 == ret)
{
perror ("read error");
break;
}
if (0 == ret)
{
printf ("客户端退出\n");
break;
}
write(socket_fd[1-i], buf, ret);
}
}
// 监听套接字
int init()
{
int listen_socket = socket(AF_INET, SOCK_STREAM, 0);
if(-1 == listen_socket)
{
perror("创建套接字失败");
return -1;
}
struct sockaddr_in addr;
memset(&addr, 0, sizeof(struct sockaddr_in));
addr.sin_family = AF_INET; /* Internet地址族 */
addr.sin_port = htons(PORT); /* 端口号 */
addr.sin_addr.s_addr = htonl(INADDR_ANY); /* IP地址, 绑定本地的所有ip地址*/
int ret = bind(listen_socket, (const struct sockaddr *)&addr, sizeof(addr));
if(-1 == ret)
{
perror("绑定失败");
return -1;
}
// 3、监听套接字
ret = listen(listen_socket, 5);
if(-1 == ret)
{
perror("监听失败");
return -1;
}
return listen_socket;
}
// 通信套接字
int myAccept(int listen_socket)
{
struct sockaddr_in client_addr;
socklen_t len = sizeof(client_addr);
int client_socket = accept(listen_socket, (struct sockaddr *)&client_addr, &len);
if (-1 == client_socket)
{
perror("accept 失败");
return -1;
}
printf ("客户端的 ip = %s, 端口 = %d\n", inet_ntoa(client_addr.sin_addr), ntohs(client_addr.sin_port));
return client_socket;
}
int main(int argc, char **argv)
{
int listen_socket = init();
if (-1 == listen_socket)
return;
long i = 0;
while (i < 2)
{
socket_fd[i] = myAccept(listen_socket);
if (-1 == socket_fd[i])
{
continue;
}
// 开辟线程为客户端服务
pthread_t thread;
pthread_create(&thread, NULL, handl_client, (void *)i);
pthread_detach(thread); // 线程分离
i++;
}
close(listen_socket);
pthread_exit(NULL);
return 0;
}
5、udp服务器
#include <stdio.h>
#include <sys/types.h> /* See NOTES */
#include <sys/socket.h>
#include <arpa/inet.h>
#include <string.h>
#include <unistd.h>
#include <pthread.h>
#define PORT 9998
void handl_client(char *buf)
{
int i;
for (i = 0; i < strlen(buf)-1; i++)
{
buf[i] += 'A' - 'a';
}
}
// 监听套接字
int init()
{
int conm_socket = socket(AF_INET, SOCK_DGRAM, 0);
if(-1 == conm_socket)
{
perror("创建套接字失败");
return -1;
}
struct sockaddr_in addr;
memset(&addr, 0, sizeof(struct sockaddr_in));
addr.sin_family = AF_INET; /* Internet地址族 */
addr.sin_port = htons(PORT); /* 端口号 */
addr.sin_addr.s_addr = htonl(INADDR_ANY); /* IP地址, 绑定本地的所有ip地址*/
int ret = bind(conm_socket, (const struct sockaddr *)&addr, sizeof(addr));
if(-1 == ret)
{
perror("绑定失败");
return -1;
}
return conm_socket;
}
int main(int argc, char **argv)
{
int conm_socket = init();
if (-1 == conm_socket)
return;
char buf[1024];
while (1)
{
struct sockaddr_in client_addr;
socklen_t len = sizeof(client_addr);
ssize_t ret = recvfrom(conm_socket, buf, 1023, 0, (struct sockaddr *)&client_addr, &len);// 收
buf[ret] = '\0';
handl_client(buf); // 处理
sendto(conm_socket, buf, ret, 0, (struct sockaddr *)&client_addr, len);// 回
}
return 0;
}
6、2、udp_client.c
#include <stdio.h>
#include <sys/types.h> /* See NOTES */
#include <sys/socket.h>
#include <arpa/inet.h>
#include <string.h>
#include <unistd.h>
#include <pthread.h>
#include <netinet/in.h>
// 子线程负责写数据
void *write_data(void *v)
{
int conm_socket = *(int*)v;
char buf[1024];
while (1)
{
struct sockaddr_in client_addr;
socklen_t len = sizeof(client_addr);
ssize_t ret = recvfrom(conm_socket, buf, 1023, 0, (struct sockaddr *)&client_addr, &len);// 收
if (-1 == ret)
{
perror ("read error");
}
if (0 == ret)
{
printf ("客户端退出\n");
break;
}
buf[ret] = '\0';
printf ("%s\n", buf);
}
}
// 主线程负责读数据
void read_data(int conm_socket, char **argv)
{
// 接收方的地址
struct sockaddr_in addr;
memset(&addr, 0, sizeof(struct sockaddr_in));
addr.sin_family = AF_INET; /* Internet地址族 */
addr.sin_port = htons(atoi(argv[2])); /*服务器用的端口号 */
addr.sin_addr.s_addr = inet_addr(argv[1]); /*服务器用的IP地址 */
char buf[1024];
while (1)
{
fgets(buf, 1024, stdin);
sendto(conm_socket, buf, strlen(buf), 0, (struct sockaddr *)&addr, sizeof(addr));
}
}
int main(int argc, char **argv)
{
// 1、创建套接字
int conn_socket = socket(AF_INET, SOCK_DGRAM, 0);
if(-1 == conn_socket)
{
perror("创建套接字失败");
return -1;
}
pthread_t thread;
pthread_create(&thread, NULL, write_data, (void *)&conn_socket);
read_data(conn_socket, argv);
close(conn_socket);
return 0;
}
7、并发.c
#include <stdio.h>
#include <sys/types.h> /* See NOTES */
#include <sys/socket.h>
#include <arpa/inet.h>
#include <string.h>
#include <unistd.h>
#include <pthread.h>
#define PORT 9998
int conm_socket;
typedef struct _msg
{
char buf[256];
struct sockaddr_in client_addr;
}Msg;
Msg task[100]; // 任务
int front = 0;
int rear = 0;
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; // 互斥锁
pthread_cond_t cond1; // 条件变量
void handl_client(Msg *data)
{
int i;
for (i = 0; i < strlen(data->buf)-1; i++)
{
data->buf[i] += 'A' - 'a';
}
sendto(conm_socket, data->buf, strlen(data->buf), 0, (struct sockaddr *)&(data->client_addr), sizeof(data->client_addr));// 回
}
// 监听套接字
int init()
{
int conm_socket = socket(AF_INET, SOCK_DGRAM, 0);
if(-1 == conm_socket)
{
perror("创建套接字失败");
return -1;
}
struct sockaddr_in addr;
memset(&addr, 0, sizeof(struct sockaddr_in));
addr.sin_family = AF_INET; /* Internet地址族 */
addr.sin_port = htons(PORT); /* 端口号 */
addr.sin_addr.s_addr = htonl(INADDR_ANY); /* IP地址, 绑定本地的所有ip地址*/
int ret = bind(conm_socket, (const struct sockaddr *)&addr, sizeof(addr));
if(-1 == ret)
{
perror("绑定失败");
return -1;
}
return conm_socket;
}
void *consume(void *v)
{
while (1)
{
Msg data;
pthread_mutex_lock(&mutex);
while (rear == front)
{
pthread_cond_wait(&cond1, &mutex);
}
front = (front+1)%20;
data = task[front];
pthread_mutex_unlock(&mutex);
handl_client(&data);
}
}
void produce()
{
char buf[1024];
while (1)
{
struct sockaddr_in client_addr;
socklen_t len = sizeof(client_addr);
ssize_t ret = recvfrom(conm_socket, buf, 1023, 0, (struct sockaddr *)&client_addr, &len);// 收
buf[ret] = '\0';
printf ("[%s:%d] %s", inet_ntoa(client_addr.sin_addr), ntohs(client_addr.sin_port), buf);
pthread_mutex_lock(&mutex);
if ((rear+1)%20 == front)
continue;
rear = (rear+1)%20;
strcpy(task[rear].buf, buf);
task[rear].client_addr = client_addr;
pthread_cond_broadcast(&cond1); // 广播
pthread_mutex_unlock(&mutex);
}
}
int main(int argc, char **argv)
{
long i;
pthread_t thread;
for (i = 0; i < 8; i++)
{
pthread_create(&thread, NULL, consume, (void*)(i+1));
pthread_detach(thread);
}
pthread_cond_init(&cond1, NULL);
conm_socket = init();
if (-1 == conm_socket)
return;
produce();
return 0;
}
8、多路复用
tcp_server.c
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <stdlib.h>
#include <sys/wait.h>
#include <sys/select.h>
#define SIZE 1024
#define PORT 9999
// sfd 用于和客户端通信
void handl_client(int sfd, fd_set *allset)
{
char buf[SIZE] = {0};
int ret = read (sfd, buf, SIZE-1);
if (ret == -1)
{
perror ("read");
return;
}
if (ret == 0)
{
printf ("客户端退出\n");
FD_CLR(sfd, allset);
return;
}
buf[ret] = '\0';
printf ("read %d bytes : %s\n", ret, buf);
int i;
for (i = 0; i < ret - 1; i++)
{
buf[i] = buf[i] + 'A' - 'a';
}
ret = write (sfd, buf, ret);
}
// 监听套接字
int init()
{
int listen_socket = socket(AF_INET, SOCK_STREAM, 0);
if(-1 == listen_socket)
{
perror("创建套接字失败");
return -1;
}
struct sockaddr_in addr;
memset(&addr, 0, sizeof(struct sockaddr_in));
addr.sin_family = AF_INET; /* Internet地址族 */
addr.sin_port = htons(PORT); /* 端口号 */
addr.sin_addr.s_addr = htonl(INADDR_ANY); /* IP地址, 绑定本地的所有ip地址*/
int ret = bind(listen_socket, (const struct sockaddr *)&addr, sizeof(addr));
if(-1 == ret)
{
perror("绑定失败");
return -1;
}
// 3、监听套接字
ret = listen(listen_socket, 5);
if(-1 == ret)
{
perror("监听失败");
return -1;
}
return listen_socket;
}
// 通信套接字
int myAccept(int listen_socket)
{
struct sockaddr_in client_addr;
socklen_t len = sizeof(client_addr);
int client_socket = accept(listen_socket, (struct sockaddr *)&client_addr, &len);
if (-1 == client_socket)
{
perror("accept 失败");
return -1;
}
printf ("客户端的 ip = %s, 端口 = %d\n", inet_ntoa(client_addr.sin_addr), ntohs(client_addr.sin_port));
return client_socket;
}
int main()
{
// 初始化套接字
int listen_fd = init();
if (listen_fd == -1)
return -1;
int maxfd = listen_fd;
fd_set allset;
fd_set rset;
FD_ZERO(&allset); // 将文件描述符集清空
FD_SET(listen_fd, &allset); // 将监听文件描述符加入到监听序列
int client_fd[FD_SETSIZE]; // 代表select能够检测客户端数量
// 对数组进行初始化,全部置为-1,凡是值为-1的位置,都是空位置
int i;
for (i = 0; i < FD_SETSIZE; i++)
{
client_fd[i] = -1;
}
while (1)
{
rset = allset;
int count = select(maxfd + 1, &rset, NULL, NULL, NULL);
if (count == -1)
{
perror ("select");
break;
}
if (FD_ISSET(listen_fd, &rset)) // 查看是否有客户端要连接
{
int conn_fd = myAccept(listen_fd);
if (conn_fd == -1)
{
continue;
}
// 找个空位置存与客户端通信的文件描述符
for (i = 0; i < FD_SETSIZE; i++)
{
if (client_fd[i] == -1) // 找到一个空位置
{
client_fd[i] = conn_fd;
break;
}
}
// 判断文件描述符集是否已达上限
if (i == FD_SETSIZE)
{
close (conn_fd);
}
else
{
FD_SET(conn_fd, &allset);
if (maxfd < conn_fd)
maxfd = conn_fd;
}
if (--count <= 0)
continue;
}
for (i = 0; i < FD_SETSIZE; i++)
{
if (client_fd[i] == -1)
continue;
if (FD_ISSET(client_fd[i], &rset))
{
handl_client(client_fd[i], &allset);
if (--count <= 0)
break;
}
}
}
close (listen_fd);
return 0;
}