socket学习笔记——实现收发文件(Windows)

记录下来,供自己学习!

server.c

 #define _CRT_SECURE_NO_DEPRECATE
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <WinSock2.h>
#define BUF_SIZE 1024
char buf[BUF_SIZE], message[BUF_SIZE], file_name[BUF_SIZE];
void error_handling(char* message);
int main(int argc, char* argv[])
{
WSADATA wsadata;
if (WSAStartup(MAKEWORD(, ), &wsadata) != )
error_handling("WSAStartup error"); SOCKET hServSock, hClntSock;
SOCKADDR_IN servAddr, clntAddr;
int clntAdrSize, length; if (argc != )
{
printf("usage:%s <ip> <port>\n", argv[]);
exit();
} hServSock = socket(PF_INET, SOCK_STREAM, );
if (hServSock == INVALID_SOCKET)
error_handling("socket error"); memset(&servAddr, , sizeof(servAddr));
servAddr.sin_family = AF_INET;
servAddr.sin_addr.S_un.S_addr = htonl(INADDR_ANY);
servAddr.sin_port = htons(atoi(argv[])); if (bind(hServSock, (SOCKADDR*)&servAddr, sizeof(servAddr)) == SOCKET_ERROR)
error_handling("bind error");
if (listen(hServSock, ) == SOCKET_ERROR)
error_handling("listen error");
while ()
{
clntAdrSize = sizeof(clntAddr);
hClntSock = accept(hServSock, (struct sockaddr*)&clntAddr, &clntAdrSize);
if (hClntSock == SOCKET_ERROR)
error_handling("accept() error");
else
printf("connecting client......\n"); if (recv(hClntSock, message, sizeof(message), ) < )
error_handling("recv error"); strncpy(file_name, message, strlen(message)); FILE *fp;
fp = fopen(file_name, "r");
if (fp == NULL)
{
printf("fopen %s error\n", file_name);
exit();
}
else
{
while ((length = fread(buf, sizeof(char), , fp)) > )
{
if (send(hClntSock, buf, length, ) < )
error_handling("send error");
else
printf("send %s successful\n", file_name);
memset(message, ,sizeof(message));
memset(file_name, , sizeof(file_name));
}
fclose(fp);
}
closesocket(hClntSock);
}
closesocket(hServSock);
WSACleanup();
return ;
} void error_handling(char* message)
{
fputs(message, stderr);
fputc('\n', stderr);
exit();
}

client.c

 #define _WINSOCK_DEPRECATED_NO_WARNINGS
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <WinSock2.h>
void error_handling(char* message);
#define BUF_SIZE 1024
int main(int argc, char* argv[])
{
WSADATA wsadata;
if (WSAStartup(MAKEWORD(, ), &wsadata) != )
error_handling("WSADTA error\n"); SOCKADDR_IN servAddr;
SOCKET hSocket;
char buf[BUF_SIZE];
char message[BUF_SIZE];
int length;
FILE* fp;
if (argc != )
{
printf("usage:%s <ip><port>", argv[]);
exit();
} hSocket = socket(PF_INET, SOCK_STREAM, );
if (hSocket == SOCKET_ERROR)
error_handling("socket error"); memset(&servAddr, , sizeof(servAddr));
servAddr.sin_family = AF_INET;
servAddr.sin_addr.S_un.S_addr = inet_addr(argv[]);
servAddr.sin_port = htons(atoi(argv[])); if (connect(hSocket, (SOCKADDR*)&servAddr, sizeof(servAddr)) == SOCKET_ERROR)
error_handling("connect error");
else
printf("connecting......\n"); fp = fopen("F:\\2.txt", "w");
if (NULL == fp)
{
error_handling("fopen error");
} printf("enter file's name: ");
scanf("%s", message);
send(hSocket, message, sizeof(message), );
while ((length = recv(hSocket, buf, BUF_SIZE, )) > )
{
printf("%d", length);
if (fwrite(buf, sizeof(char), length, fp) < length)
{
printf("fwrite error");
exit();
}
else
{
printf("receve successful\n");
}
}
fclose(fp);
closesocket(hSocket);
WSACleanup();
return ;
}
void error_handling(char* message)
{
fputs(message, stderr);
fputc('\n', stderr);
exit();
}
上一篇:clnt_create: RPC: Program not registered


下一篇:C# Socket学习笔记一