Linux网络编程(三)——select函数实现I/O复用(传输文件)

客户端

#include <stdio.h>
#include <stdlib.h>
#include <WinSock2.h>
#include<io.h>
#include<time.h>
void error(char *er);
#pragma comment(lib, "ws2_32.lib")  //加载 ws2_32.dll
int main(){
	while (1)
	{

	char IP[100];
	int port;
	//初始化DLL
	WSADATA wsaData;
	if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0)
		error("WSAStartup error!\n");
	//创建套接字
	SOCKET sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
	//向服务器发起请求
	if (sock == INVALID_SOCKET)
		error("Wrong scoket!\n");
	printf("输入目标IP地址和端口号(ip/网址 port):");
	scanf("%s %d", IP, &port);
	if (!(IP[0] >= 0 && IP[0] <= 9))//输入为域名的情况!
	{
		struct hostent*host;
		host=gethostbyname(IP);
		strcpy(IP, inet_ntoa(*(struct in_addr*)host->h_addr_list[0]));
	}

	sockaddr_in sockAddr;
	memset(&sockAddr, 0, sizeof(sockAddr));  //每个字节都用0填充
	sockAddr.sin_family = PF_INET;
	sockAddr.sin_addr.s_addr = inet_addr(IP);
	sockAddr.sin_port = htons(port);
	//发送命令
	if (connect(sock, (SOCKADDR*)&sockAddr, sizeof(SOCKADDR)) == SOCKET_ERROR)//连接
		error("Connect failed!\n");
	//接收服务器传回的数据
	int len;
	char szBuffer[MAXBYTE] = "It's hard to see you!\n";
	//发送文件
	FILE*p = fopen("C:\\Users\\Administrator\\Desktop\\1.txt", "rb+");
	if (p == NULL)
	{
		close(sock);
		printf("fopen()  error!\n");
		return 0;
		system("pause");
	}
	while ((len = fread(szBuffer, 1, 100, p)) > 0)
	{
		szBuffer[len] = 0;
		printf("%s", szBuffer);
		send(sock, szBuffer, len, 0);
	}
	fclose(p);
	if (len == -1)
		error("send\n");
	shutdown(sock, SD_SEND);
	
	len = recv(sock, szBuffer, MAXBYTE, 0);
	//输出接收到的数据
		if (len > 0)
		printf("\nMessage form server: %s\n", szBuffer);
		else
		{
			closesocket(sock); break;
			printf("No message!\n");
		}
		
		//关闭套接字
		closesocket(sock);
	}
	//终止使用 DLL
	WSACleanup();
	system("pause");
	return 0;
}
void error(char *er)
{
	printf("%s\n", er);
	exit(1);
}




服务器端


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <sys/wait.h>
#include <signal.h>
#include <fcntl.h>
#include <time.h>
#include <sys/select.h>


#define BUF_SIZE 1024
void error_handling(char *message);
void childproc(int sig);
int main(int argc, char *argv[])
{
	struct timeval timeout;
	fd_set reads, cpy_reads;
	int fd_max, strlen, fd_num;
	int serv_sock, clnt_sock;
	int str_len, i = 0;
	struct sockaddr_in serv_adr;
	struct sockaddr_in clnt_adr;
	socklen_t clnt_adr_sz;

	if (argc != 2) {
		printf("Usage : %s <port>\n", argv[0]);
		exit(1);
	}
	serv_sock = socket(PF_INET, SOCK_STREAM, 0);
	if (serv_sock == -1)
		error_handling("socket() error");
	memset(&serv_adr, 0, sizeof(serv_adr));
	serv_adr.sin_family = AF_INET;
	serv_adr.sin_addr.s_addr = htonl(INADDR_ANY);
	serv_adr.sin_port = htons(atoi(argv[1]));
	if (bind(serv_sock, (struct sockaddr*)&serv_adr, sizeof(serv_adr)) == -1)
	{
		error_handling("bind() error"); exit(1);
	}


	if (listen(serv_sock, 5) == -1)
		exit(1);
	FD_ZERO(&reads);
	FD_SET(serv_sock, &reads);
	fd_max = serv_sock;
	printf("serv_sock:%d\n", serv_sock);

	while (1){
		printf("start!\n");
		cpy_reads = reads;
		timeout.tv_sec = 10;
		timeout.tv_usec = 5000;
		if ((fd_num = select(fd_max + 1,&cpy_reads,0,0,  &timeout)) == -1)//error
			break;
		if (fd_num == 0)//overtime
			{printf("overtime\n");continue;}
		for (i = 0; i < fd_max + 1; i++)
		{
			printf("i:%d\n",i);
			printf("fd_num:%d\n",fd_num);
			if (FD_ISSET(i, &cpy_reads))
			{
				if (i == serv_sock)
				{
					clnt_adr_sz = sizeof(clnt_adr);

					clnt_sock = accept(serv_sock, (struct sockaddr*)&clnt_adr, &clnt_adr_sz);
					FD_SET(clnt_sock,&reads);
					printf("clnt_sock:%d\n",clnt_sock);
					if (fd_max < clnt_sock)fd_max = clnt_sock;
					printf("changed: fd_max:%d\n",fd_max);
					printf("\nConnected client id: %d \n", clnt_sock);				
				}//if(i==serv_sock)
				else
				{
					int len=0;
					char buff[100]={0};
					int file=open("test.c",O_CREAT|O_WRONLY);
					while((len=read(clnt_sock,buff,100))>0)//get files
					{
						printf("len:%d\n",len);
						write(file,buff,len);
					}
					close(file);
					//sleep(10);
					if(len==-1)
						error_handling("read() error!");
					strcpy(buff,"successfully!\n");
					printf("read message:%s\n",buff);
					if((len=write(clnt_sock,buff,100))==-1)
						error_handling("write() error!");
					printf("nums:%d\n",len);
			
					printf("write message:%s\n",buff);
					//service
					
					FD_CLR(i,&reads);//clear
					printf("read1\n");
				}//else
			}//if(FD_ISSET(i.&cpy_reads))
		}//for
	}//while
	close(serv_sock);
	return 0;
}
void error_handling(char *message)
{
	fputs(message, stderr);
	fputc('\n', stderr);
}



你可能感兴趣的:(linux,IO,C语言,网络编程,select)