我有一个 UDP 客户端和一个 UDP 服务器。程序的流程是:首先从终端执行服务器,终端创建一个套接字并绑定它并等待来自客户端的文件名。在另一个终端中执行客户端。这里还创建了一个套接字,并与服务器建立了连接。然后将文件名提供给客户端。使用 sendto() 函数将此文件名发送到服务器。服务器能够从客户端接收文件名,并且服务器还将文件中的数据发送给客户端。然而,另一端的接收者一直在等待来自服务器的数据。
UDP 客户端和服务器的代码如下所示。
UDP 服务器:
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<sys/stat.h>
#include<unistd.h>
#include<stdlib.h>
#include<stdio.h>
#include<fcntl.h>
int main()
{
int cont,create_socket,new_socket,addrlen,fd;
int bufsize = 1024;
int nameLen=0;
int client_address_size=0;
char *buffer = malloc(10);
char fname[256];
struct sockaddr_in address,client;
if ((create_socket = socket(AF_INET,SOCK_DGRAM,0)) > 0)
printf("The socket was created\n");
address.sin_family = AF_INET;
address.sin_addr.s_addr = INADDR_ANY;
address.sin_port = htons(15000);
if (bind(create_socket,(struct sockaddr *)&address,sizeof(address))== 0)
printf("Binding Socket\n");
nameLen=sizeof(address);
if (getsockname(create_socket,(struct sockaddr *)&address,&nameLen)<0)
{
printf("\n\ngetsockname() error\n");
exit(3);
}
printf("Port assigned is %d\n", ntohs(address.sin_port));
client_address_size=sizeof(client);
if(recvfrom(create_socket,fname, 255,0,(struct sockaddr *) &client,&client_address_size)<0)
{
printf("\n\nrecvfrom() failed\n");
exit(4);
}
printf("A request for filename %s Received..\n", fname);
if ((fd=open(fname, O_RDONLY))<0)
{
perror("File Open Failed");
exit(0);
}
while((cont=read(fd, buffer, 10))>0)
{
//sleep(1);
sendto(create_socket,buffer,cont,0,(struct sockaddr *) &client,&client_address_size);
printf("\n\nPacket sent\n");
}
printf("Request Completed\n");
return close(create_socket);
}
UDP客户端:
#include<sys/socket.h>
#include<sys/types.h>
#include<netinet/in.h>
#include<unistd.h>
#include<stdlib.h>
#include<stdio.h>
int main()
{
int create_socket,cont;
char *arg="127.0.0.1";
int bufsize = 1024;
int server_address_size=0;
char *buffer = malloc(10);
char fname[256];
struct sockaddr_in address,server;
if ((create_socket = socket(AF_INET,SOCK_DGRAM,0)) > 0)
printf("The Socket was created\n");
address.sin_family = AF_INET;
address.sin_port = htons(15000);
address.sin_addr.s_addr=inet_addr(arg);
if (connect(create_socket,(struct sockaddr *) &address,sizeof(address)) == 0)
printf("The connection was accepted with the server %s...\n",arg);
printf("Enter The Filename to Request : ");
scanf("%s",fname);
sendto(create_socket, fname, sizeof(fname), 0,(struct sockaddr *) &address,sizeof(address));
printf("Request Accepted... Receiving File...\n\n");
server_address_size=sizeof(server);
printf("The contents of file are...\n\n");
while((cont=recvfrom(create_socket, buffer, 10, 0,(struct sockaddr *) &address,sizeof(address)))>0)
{
write(1, buffer, cont);
}
printf("\nEOF\n");
return close(create_socket);
}
我哪里错了?请为此提供适当的解决方案。
提前致谢。