一、实验目的和要求
1 了解TCP/IP协议
2 掌握Socket编程,熟悉基于TCP和UDP的传输模型
3 掌握多线程编程
4 掌握基于TCP的并发服务器设计
二、实验内容和原理
实验内容:编写C程序,利用多线程构建TCP并发服务器,并实现客户端和服务器的传输(多个并发用户同时访问服务器)
实验原理:TCP的传输模型和线程的并发执行
三、主要仪器设备
PC机、装有Linux操作系统的虚拟机
四、操作方法与实验步骤
线程并发客户端:
#include <netinet/in. h> // for sockaddr_ in
#include <sys/types. h> // for socket
#include <sys/ socket. h> // for socket
#include <stdio. h> // for printf :
#include <stdlib. h> . // for exit
#include <string. h> // for bzero
#include <pthread. h>
#include <sys/errno. h> // for errno
#define HELLO_WORLD_SERVER_PORT 6666
#define BUFFER_SIZE 1024
char * server_IP = NULL;
void * talk_to_server(void * thread_num)
{
struct sockaddr_in c1ient_addr ;
bzero (&c1ient_addr, sizeof (c1ient_addr)); //把一段内存区