C Beej Unix套接字发送/接收错误
我有一个client.c和server.c程序,用于执行以下操作:客户端向服务器发送一条写有“Get\n”的消息,服务器向客户端发送一个文件。文件传输完成后,连接将关闭 到目前为止,一切都正常,直到服务器应该接收“Get\n”为止,我似乎不知道为什么。我有很多printf()语句,所以我可以直观地解释所有发生的事情,但这并没有帮助我解决这个问题,所以我真的希望有人能在这里帮助我 这是我客户的代码。c:C Beej Unix套接字发送/接收错误,c,sockets,unix,networking,tcp,C,Sockets,Unix,Networking,Tcp,我有一个client.c和server.c程序,用于执行以下操作:客户端向服务器发送一条写有“Get\n”的消息,服务器向客户端发送一个文件。文件传输完成后,连接将关闭 到目前为止,一切都正常,直到服务器应该接收“Get\n”为止,我似乎不知道为什么。我有很多printf()语句,所以我可以直观地解释所有发生的事情,但这并没有帮助我解决这个问题,所以我真的希望有人能在这里帮助我 这是我客户的代码。c: #include <stdlib.h> #include <stdio.h&
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <sys/wait.h>
#include <sys/socket.h>
#include <signal.h>
#include <ctype.h>
#include <arpa/inet.h>
#include <netdb.h>
#define PORT 43001
#define LENGTH 512
int main(int argc, char *argv[])
{
int sockFD, newSockFD;
char buff[LENGTH];
struct sockaddr_in remote;
//Get Socket Descriptor
if((sockFD = socket(AF_INET, SOCK_STREAM, 0)) == -1)
{
printf("Failed to get socket descriptor\n");
exit(1);
}
//Fill sockaddr struct
remote.sin_family = AF_INET;
remote.sin_port = htons(PORT);
inet_pton(AF_INET, "127.0.0.1", &remote.sin_addr);
bzero(&(remote.sin_zero), 8);
//Connect to server
if(connect(sockFD, (struct sockaddr *)&remote, sizeof(struct sockaddr)) == -1)
{
printf("Failed to connect()\n");
exit(1);
}
else
{
printf("Connect()ed!\n");
}
//Send message
char *msg = "Get\n";
int len, sentBytes;
len = strlen(msg);
printf("Sending: %s\n", msg);
if((sentBytes = send(sockFD, msg, len, 0)) == -1)
{
printf("Error in sending message\n");
exit(1);
}
else
{
printf("Message sent successfully!\n");
}
//Recieve File
printf("Getting file from Server\n");
char* fr_name = "/in.txt";
FILE *fr = fopen(fr_name, "w");
if(fr == NULL)
{
printf("File could not be opened\n");
}
else
{
bzero(buff, LENGTH);
int inBlockSize = 0;
while((inBlockSize = recv(sockFD, buff, LENGTH, 0)) > 0)
{
int writeSize = fwrite(buff, sizeof(char), inBlockSize, fr);
if(writeSize < inBlockSize)
{
printf("File writing failed\n");
}
bzero(buff, LENGTH);
if(inBlockSize == 0 || inBlockSize != 512)
{
break;
}
if(inBlockSize < 0)
{
printf("recv() failed due to some error\n");
}
}
printf("OK received from server\n");
fclose(fr);
}
close(sockFD);
printf("Connection closed\n");
return 0;
}
然后,对于server.c,这是我的代码:
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <sys/wait.h>
#include <sys/socket.h>
#include <signal.h>
#include <ctype.h>
#include <arpa/inet.h>
#include <netdb.h>
#define PORT 43001
#define BACKLOG 5
#define LENGTH 512
int main()
{
int sockFD, newSockFD;
int num;
int sin_size;
struct sockaddr_in local;
struct sockaddr_in remote;
char buff[LENGTH];
char* fileName = "1.txt";
char sendBuff[LENGTH];
FILE *fs;
//Get socket descriptor
if ((sockFD = socket(AF_INET, SOCK_STREAM, 0)) == -1)
{
printf("Failed to get socket descriptor.\n");
exit(1);
}
else
{
printf("Got socket descriptor successfully\n");
}
//Get client socket struct
local.sin_family = AF_INET;
local.sin_port = htons(PORT);
local.sin_addr.s_addr = INADDR_ANY;
bzero(&(local.sin_zero), 8); //Deals with the rest of the struct
//Bind
if(bind(sockFD, (struct sockaddr*)&local, sizeof(struct sockaddr)) == -1)
{
printf("Failed to bind to port\n");
exit(1);
}
else
{
printf("Binded to port successfully\n");
}
//Listen
if(listen(sockFD, BACKLOG) == -1)
{
printf("listen() failed\n");
exit(1);
}
else
{
printf("listen()ing to port successfully\n");
}
sin_size = sizeof(struct sockaddr_in);
//Accept
if(newSockFD = accept(sockFD, (struct sockaddr *)&remote, &sin_size) == -1)
{
printf("accept() failed\n");
exit(1);
}
else
{
printf("accept()'d connection successfully\n");
}
//Get Message from Client
int blkSize = 0;
if((blkSize = recv(newSockFD, buff, LENGTH, 0)) == -1)
{
printf("recv()ing failed\n");
exit(1);
}
buff[blkSize + 1] = "\0";
printf("Client sent: %s\n", buff);
//Open File Contents
printf("Sending file to client\n");
fs = fopen(fileName, "r");
if(fs == NULL)
{
printf("File not found\n");
exit(1);
}
bzero(sendBuff, LENGTH);
int sendingBlkSize;
//Send File
while((sendingBlkSize = fread(sendBuff, sizeof(char), LENGTH, fs)) > 0)
{
if(send(newSockFD, sendBuff, sendingBlkSize, 0) < 0)
{
printf("Failed to send the file\n");
exit(1);
}
bzero(sendBuff, LENGTH);
}
printf("Sent file to client, closing connection\n");
close(newSockFD);
return 0;
}
因此,消息已成功发送,但另一端未收到。我已经尽了最大努力来解决这个问题,但我觉得一切都很好。我正在使用Beej的《网络编程指南》在Unix上实现这一点。考虑您试图打开的文件的路径,
/in.txt
。您在根目录中真的有写权限吗?另外请刷新,尤其是和。同时,也可以阅读一些好的问题和建议。你问的问题是什么?因为显然有两个截然不同的问题。别忘了你的小部分。我理解为什么我会在那里出错,我应该在发布之前修复它,所以很抱歉。另外,你能澄清一下这两个不同的问题是什么意思吗?我相信我遇到的唯一问题是当我的server.c程序收到一条消息时,但是如果你看到另一条消息,我很想知道它可能是什么!使用perror显示故障原因。不管beej怎么说。(最可能的问题是客户端在服务器读取数据之前终止连接。)
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <sys/wait.h>
#include <sys/socket.h>
#include <signal.h>
#include <ctype.h>
#include <arpa/inet.h>
#include <netdb.h>
#define PORT 43001
#define BACKLOG 5
#define LENGTH 512
int main()
{
int sockFD, newSockFD;
int num;
int sin_size;
struct sockaddr_in local;
struct sockaddr_in remote;
char buff[LENGTH];
char* fileName = "1.txt";
char sendBuff[LENGTH];
FILE *fs;
//Get socket descriptor
if ((sockFD = socket(AF_INET, SOCK_STREAM, 0)) == -1)
{
printf("Failed to get socket descriptor.\n");
exit(1);
}
else
{
printf("Got socket descriptor successfully\n");
}
//Get client socket struct
local.sin_family = AF_INET;
local.sin_port = htons(PORT);
local.sin_addr.s_addr = INADDR_ANY;
bzero(&(local.sin_zero), 8); //Deals with the rest of the struct
//Bind
if(bind(sockFD, (struct sockaddr*)&local, sizeof(struct sockaddr)) == -1)
{
printf("Failed to bind to port\n");
exit(1);
}
else
{
printf("Binded to port successfully\n");
}
//Listen
if(listen(sockFD, BACKLOG) == -1)
{
printf("listen() failed\n");
exit(1);
}
else
{
printf("listen()ing to port successfully\n");
}
sin_size = sizeof(struct sockaddr_in);
//Accept
if(newSockFD = accept(sockFD, (struct sockaddr *)&remote, &sin_size) == -1)
{
printf("accept() failed\n");
exit(1);
}
else
{
printf("accept()'d connection successfully\n");
}
//Get Message from Client
int blkSize = 0;
if((blkSize = recv(newSockFD, buff, LENGTH, 0)) == -1)
{
printf("recv()ing failed\n");
exit(1);
}
buff[blkSize + 1] = "\0";
printf("Client sent: %s\n", buff);
//Open File Contents
printf("Sending file to client\n");
fs = fopen(fileName, "r");
if(fs == NULL)
{
printf("File not found\n");
exit(1);
}
bzero(sendBuff, LENGTH);
int sendingBlkSize;
//Send File
while((sendingBlkSize = fread(sendBuff, sizeof(char), LENGTH, fs)) > 0)
{
if(send(newSockFD, sendBuff, sendingBlkSize, 0) < 0)
{
printf("Failed to send the file\n");
exit(1);
}
bzero(sendBuff, LENGTH);
}
printf("Sent file to client, closing connection\n");
close(newSockFD);
return 0;
}
Got socket descriptor successfully
Binded to port successfully
listen()ing to port successfully
accept()'ed connection successfully
recv()ing failed