通过TCP | C、Windows发送文件
你好,我正在尝试将文件从服务器发送到客户端 我确实发了一条信息,它确实起了作用 但对于文件来说,它就是不起作用。。 可能是语法有问题 你知道为什么吗 客户:通过TCP | C、Windows发送文件,c,file,tcp,client,server,C,File,Tcp,Client,Server,你好,我正在尝试将文件从服务器发送到客户端 我确实发了一条信息,它确实起了作用 但对于文件来说,它就是不起作用。。 可能是语法有问题 你知道为什么吗 客户: #include<stdio.h> #include<winsock2.h> #include <stdlib.h> #include <Windows.h> #include <string.h> #pragma comment(lib,"ws2_32.lib") //Wins
#include<stdio.h>
#include<winsock2.h>
#include <stdlib.h>
#include <Windows.h>
#include <string.h>
#pragma comment(lib,"ws2_32.lib") //Winsock Library
int main()
{
WSADATA wsa;// Struct that winsock2 has
SOCKET s; // the socket
size_t nRecv,nWrite;
FILE *fp;
struct sockaddr_in server; // built-in struct
/*struct sockaddre_in
{
short sin_family; // Address Faimly (IPV4 / IPV6)
unsigned short sin_port; // which port the data will transfer e.g htons(80)
struct in_addr sin_addr;
char sin_zero[8];
}server;*/
char *message , server_reply[2000]; // the data we send and the server reply
int recv_size; // the size of the recived data
// ------------------------------------------Checking the DLL verision --------------------------------------- //
// - First we have to initialising the winsock dll to see if there are any errors while loading it //
printf("\nInitialising Winsock...");
if (WSAStartup(MAKEWORD(2,2),&wsa) != 0) // if there are any error WSAStartup will return non-zero value .
{
printf("Failed. Error Code : %d",WSAGetLastError());
return 1;
}
printf("Step 1 : Initialised.\n");
// ---------------------------------------------Creating the Socket ----------------------------------------- //
// - AF_INET - Address Family (IP version , This case is IPV4)
//- SOCK_STREAM - this is the type , what type of communction would be this case is TCP Which could STREAM
// - 0 is the TCP protocol.
if((s = socket(AF_INET , SOCK_STREAM , 0 )) == INVALID_SOCKET) // if the returned value of socket() INVALID_SOCKET
{
printf("Could not create socket : %d" , WSAGetLastError());
return 1;
}
printf("Step 2 : Socket created.\n");
// --------------------------------------- Connecting to the SERVER ----------------------------------------//
// inet_addr() converting a IP to a long format .
server.sin_addr.s_addr = inet_addr("10.0.0.4"); // <-- here we have to insert the ip of the server
server.sin_family = AF_INET; // IPV4 in this case .
server.sin_port = htons( 9999 ); // the wanted port (HTML = 80)
//Connect to remote server
if (connect(s , (struct sockaddr *)&server , sizeof(server)) < 0)//casing server value to sockaddr struct
{
puts("connect error");
return 1;
}
puts("Step 3 : Connected");
//-----------------------------------------------------Recieve from the Server ------------------------------//
fp = fopen("output.txt", "wb");
if (fp == NULL)
{
printf("File not found!\n");
return 1;
}
else
{
printf("created file output.txt\n");
}
while (recv_size = recv(s, server_reply, 256, 0))
{
if (recv_size == SOCKET_ERROR) {
puts("recv failed");
return 1;
}
else
{
nWrite = fwrite(server_reply, sizeof(char), recv_size, fp);
}
if(nWrite<=0)
{
printf("error while reading the file");
return 1;
}
else{
puts("Step 4 :Reply received\n");
}
}
return 0;
}
#包括
#包括
#包括
#包括
#包括
#pragma注释(lib,“ws2_32.lib”)//Winsock库
int main()
{
WSADATA wsa;//winsock2具有的结构
套接字s;//套接字
尺寸为nRecv,nWrite;
文件*fp;
服务器中的struct sockaddr_;//内置结构
/*结构sockaddre_in
{
短sinu系列;//地址Faimly(IPV4/IPV6)
无符号短单端口;//数据将传输到哪个端口,例如htons(80)
结构输入地址sin地址;
char sin_zero[8];
}服务器*/
char*message,server_reply[2000];//我们发送的数据和服务器回复
int recv_size;//接收数据的大小
//---------------------------------------正在检查DLL验证--------------------//
//-首先,我们必须初始化winsock dll,查看加载时是否有任何错误//
printf(“\n初始化Winsock…”);
if(WSAStartup(MAKEWORD(2,2),&wsa)!=0)//如果有任何错误,WSAStartup将返回非零值。
{
printf(“失败。错误代码:%d”,WSAGetLastError());
返回1;
}
printf(“步骤1:初始化。\n”);
//---------------------------------------------------创建套接字--------------------------------//
//-AF_INET-地址系列(IP版本,本例为IPV4)
//-SOCK_STREAM-这是一种类型,什么类型的通信会是这种情况下的TCP可以流
//-0是TCP协议。
if((s=socket(AF\u INET,SOCK\u STREAM,0))==无效的\u socket)//如果socket()的返回值无效的\u socket
{
printf(“无法创建套接字:%d”,WSAGetLastError());
返回1;
}
printf(“步骤2:创建套接字。\n”);
//------------------------------------连接到服务器----------------------------------------//
//inet_addr()将IP转换为长格式。
server.sin_addr.s_addr=inet_addr(“10.0.0.4”);//客户端:'nWrite=fwrite(server_reply,sizeof(char),nRecv,fp);'-nRecv未初始化。你是说'recv_size'?还有,几乎不可避免的问题:你在自己的调试工作中发现了什么?为什么海报不告诉我们这些东西?@MartinJames,是的,对不起。调试错误:表达式(steam!=NULL)。读取时出错。无法读取文件
#include<io.h>
#include<stdio.h>
#include<winsock2.h>
#pragma comment(lib,"ws2_32.lib") //Winsock Library
int main()
{
File *fp;
WSADATA wsa;
SOCKET s , new_socket;
struct sockaddr_in server , client;
int c;
char message[200],TheData[256];
size_t nRead,nSent
printf("\nInitialising Winsock...");
if (WSAStartup(MAKEWORD(2,2),&wsa) != 0)
{
printf("Failed. Error Code : %d",WSAGetLastError());
return 1;
}
printf("Initialised.\n");
//Create a socket
if((s = socket(AF_INET , SOCK_STREAM , 0 )) == INVALID_SOCKET)
{
printf("Could not create socket : %d" , WSAGetLastError());
}
printf("Socket created.\n");
//Prepare the sockaddr_in structure
server.sin_family = AF_INET;
server.sin_addr.s_addr = inet_addr("10.0.0.4");
server.sin_port = htons( 9999 );
//Bind
if( bind(s ,(struct sockaddr *)&server , sizeof(server)) == SOCKET_ERROR)
{
printf("Bind failed with error code : %d" , WSAGetLastError());
return 1;
}
puts("Bind done");
//Listen to incoming connections
listen(s , 3);
//Accept and incoming connection
puts("Waiting for incoming connections...");
c = sizeof(struct sockaddr_in);
while ((new_socket = accept(s , (struct sockaddr *)&client, &c)) != INVALID_SOCKET) {
printf ("Server: ");
pf = fopen("input.txt", "rb");
while (!feof(pf))
{
nRead = fread(TheData, sizeof(char), 256, pf);
if (nRead <= 0)
{
printf("ERROR reading file");
return 1;
}
while (nRead > 0)
{
nSent = send(new_socket, TheData, nRead, 0);
if (nSent < 0)
{
printf("ERROR sending from socket = %d\n", WSAGetLastError());
return 1;
}
if (nSent == 0)
{
printf("DISCONNECTED writing to socket");
return 1;
}
}
closesocket(s);
WSACleanup();
return 0;
}