//< >引用的是编译器的类库路径里面的头文件
//" "引用的是你程序目录的相对路径中的头文件#include <stdio.h>#include <stdlib.h>#include <winsock2.h>#include <string.h> //添加默认链接库#pragma comment(lib,"ws2_32.lib")#define PORT 9999
#define IPADDR "127.0.0.1"#define BACKLOG 20#define FILENAME 200#define LENGTH 200#define BUFFERSIZE 1024struct FILEHEAD //文件信息结构
{ char path_buffer[LENGTH];//文件绝对路径加完整名 char filename[LENGTH];//文件名 char ext[LENGTH];//文件扩展名 unsigned int length;//文件长度};FILEHEAD file; //全局文件信息头
struct sockaddr_in clientaddr; //定义外部变量供线程函数调用//得到文件头信息,使用全局变量函数没有参数
void getFileInformation(){ printf( "file information :\n" ); //printf( " Drive: %s\n", drive ); //printf( " Dir: %s\n", dir ); printf( " Filename: %s\n", file.filename ); printf( " Ext: %s\n", file.ext ); printf( " length is btye: %ld btye\n", file.length );}void showClientinfo()
{ //获取当前系统时间 SYSTEMTIME st; GetLocalTime(&st); char SysDate[30];//将systime中的时间转变为字符串存入SysDate[30];
sprintf(SysDate, "%4d-%2d-%2d %2d:%2d:%2d", st.wYear, st.wMonth, st.wDay, st.wHour, st.wMinute, st.wSecond); //Server显示客户端信息 printf("%s Recv from Client [%s:%d] : %s\n", SysDate, inet_ntoa(clientaddr.sin_addr), ntohs(clientaddr.sin_port)); //服务器向客户端回显信息}DWORD WINAPI RequestThread(LPVOID lparam)//线程函数,并发接收请求
{ SOCKET newsock = (SOCKET)(LPVOID)lparam; char buf[BUFFERSIZE]={0};//接收缓冲区 memset(buf,0,BUFFERSIZE);//缓冲区清零showClientinfo();//显示连接信息
printf("等待接收文件头信息...\n");
int length_file_info=recv(newsock,(char *)&file,BUFFERSIZE,0); if (length_file_info == SOCKET_ERROR) { printf("recv failed !\n"); closesocket(newsock); WSACleanup(); return -1; }if (length_file_info<=0)
{ exit(0);//没有成功接收文件头信息 }getFileInformation();//打印文件信息
//拼接文件名为receive.文件后缀 char *s="receive"; memset(file.filename,0,LENGTH); strcpy(file.filename,s); strcat(file.filename,file.ext);FILE *fp=NULL;
fp=fopen(file.filename,"wb+"); if (NULL==fp) { perror("fail to build the file!!!\n"); exit(0); }printf("要接收的文件名为:");
printf(file.filename);//打印文件名 printf ("\n catch file now....\n"); int relength=0;//接收到字节的长度Sleep(500);
printf("开始接收...\n");while (1)
{ relength=recv(newsock,buf,BUFFERSIZE,0); if (relength == SOCKET_ERROR) { printf("recv failed !\n"); closesocket(newsock); WSACleanup(); return -1; }fwrite(buf,1,relength,fp);
if (0==relength) { break; } printf("."); } printf("\n接收完成...\n");fflush(fp);
fclose(fp); fp=NULL;return 0;
}
int main(int argc,char *argv[]){ //初始化winsock版本信息,加载动态链接库(dll) WSADATA wsData; if (WSAStartup(MAKEWORD(2,2),&wsData)!=0) { printf("WSAStartup failed !!!\n"); return -1; }//创建监听套接字
SOCKET socklisten; if((socklisten=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP))==INVALID_SOCKET) { printf("socket failed!!!\n"); WSACleanup(); return -1; }//设置服务器地址
struct sockaddr_in servaddr;memset(&servaddr,0,sizeof(servaddr));
servaddr.sin_family=AF_INET; servaddr.sin_port=htons(9999); servaddr.sin_addr.S_un.S_addr=inet_addr(IPADDR);//绑定socket地址结构到监听套接字
if (bind(socklisten,(sockaddr *)&servaddr,sizeof(servaddr))!=0) { printf("binding failed!!!\n"); closesocket(socklisten); WSACleanup(); }//在server上运行监听
if (listen(socklisten,20)!=0) { printf("listen failed !!!\n"); closesocket(socklisten); WSACleanup(); return -1; }//接收客户端的连接请求
printf("TCP server is start!!!\n");//clientaddrlength要有初值,
int client_addr_length = sizeof(clientaddr); memset(&clientaddr,0,client_addr_length); SOCKET connect;//循环等待
while (1) { if ((connect=accept(socklisten,(sockaddr *)&clientaddr,&client_addr_length))==INVALID_SOCKET) { printf("accept failed!!\n"); closesocket(connect); WSACleanup(); return -1; }//创建新线程
DWORD ThreadID; CreateThread(NULL,0,RequestThread,(LPVOID)connect,0,&ThreadID); }
}
#include <stdio.h>
#include <windows.h>#include <stdlib.h>#define FILENAME 200
#define BUFFER 1024#define PORT 9999 #pragma comment(lib, "ws2_32.lib")#define LENGTH 200
struct FILEHEAD //文件信息结构{ char path_buffer[LENGTH]; //文件绝对路径 char filename[LENGTH]; //文件名 char ext[LENGTH]; //文件扩展名 unsigned int length; //文件长度};FILEHEAD file;
void setFileInformation()
{ memset(&file,0,sizeof(FILEHEAD)); //_makepath( path_buffer, "c", "\\sample\\crt\\", "makepath", "c" ); printf( "input the path and name : \n"); scanf("%s",file.path_buffer); _splitpath( file.path_buffer, NULL, NULL, file.filename, file.ext ); FILE *fp= NULL; fp=fopen(file.path_buffer,"r"); if (NULL==fp) { printf("cannot open the %s \n",file.path_buffer); exit(0); } fseek(fp,0l,SEEK_END); file.length=ftell(fp); //fclose(fp); //fp = NULL; //需要指向空,否则会指向原打开文件地址 printf( "file head information:\n" ); //printf( " Drive: %s\n", drive ); //printf( " Dir: %s\n", dir ); printf( " Filename: %s\n", file.filename ); printf( " Ext: %s\n", file.ext ); printf( " length is btye: %ld btye\n", file.length );}
int main(int argc, char *argv[])
{ WSADATA wsadata; WSAStartup(MAKEWORD(2,0),&wsadata); SOCKET sock_trans=socket(AF_INET,SOCK_STREAM,0); if (sock_trans==0) { printf ("socket build faile!!\n"); exit(1); } printf ("target pc's IP address: 127.0.0.1\n"); //如果正式调试,将服务器端ip写死。 SOCKADDR_IN sockadd; sockadd.sin_family=AF_INET; sockadd.sin_port=htons(PORT); sockadd.sin_addr.S_un.S_addr=inet_addr("127.0.0.1"); int con_info=connect(sock_trans,(SOCKADDR*)&sockadd,sizeof(sockadd)); if (con_info==SOCKET_ERROR) { printf ("connect fail!\n"); //exit(2); } Sleep(1000); setFileInformation();FILE *fp;
fp=fopen(file.path_buffer,"rb"); if (fp==NULL) { perror("\nopen file error:"); exit(1); } //freed_return_val=fread(buf,1,1024,fp);//读文件字符 char buf[BUFFER]; int fread_return_val=0;//文件读出字符的返回值//发送文件头信息
printf("发送文件头信息...\n"); send(sock_trans,(char *)&file,sizeof(file),0);printf("发送文件信息...\n");
while (1) { fread_return_val=fread(buf,1,1024,fp); if (0==fread_return_val) { break; } //freed_return_val=fread(buf,1,1024,fp); send(sock_trans,buf,fread_return_val,0); printf("."); } fclose(fp); closesocket(sock_trans); WSACleanup(); printf("\n发送完成...\n"); return 0;}