本文介绍了如何使用tcp / ip协议双向双向发送和接收文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想读取一个文件并将其发送到客户端,客户端必须接收该文件并将相同的文件发送回服务器。我已经开发了代码,但我无法发送和接收数据。有人可以指导我整理这个问题吗?我提供的代码供进一步参考。

I would like to read a file and send it to client, the client has to receive the file and send the same file back to server. I have developed the code, but i am not able to send and receive data. Can someone guide me in sorting this issue out? I am giving the code for further reference.

/****************** SERVER CODE ****************/

#include <stdio.h>
#include <string.h>
#include <sys/socket.h>
#include <netinet/in.h>

int main()
{
  int socket_desc, newSocket;
  char buffer[1024];
  struct sockaddr_in serverAddr;
  struct sockaddr_storage serverStorage;
  socklen_t addr_size;

  /*---- Create the socket. The three arguments are: ----*/
  /* 1) Internet domain 2) Stream socket 3) Default protocol (TCP in this case) */

  socket_desc = socket(AF_INET, SOCK_STREAM, 0);
  
  /*---- Configure settings of the server address struct ----*/
  /* Address family = Internet */

  serverAddr.sin_family = AF_INET;

  /* Set port number, using htons function to use proper byte order */

  serverAddr.sin_port = htons(6777);

  /* Set IP address to localhost */

  serverAddr.sin_addr.s_addr = inet_addr("127.0.0.1");

  /* Set all bits of the padding field to 0 */

  memset(serverAddr.sin_zero, '\0', sizeof serverAddr.sin_zero);  

  /*---- Bind the address struct to the socket ----*/

  bind(socket_desc, (struct sockaddr *) &serverAddr, sizeof(serverAddr));
  puts("Bind Completed:");

  /*---- Listen on the socket, with 5 max connection requests queued ----*/

  if(listen(socket_desc,5)==0)
    printf("Listening\n");
  else
    printf("Error\n");

  /*---- Accept call creates a new socket for the incoming connection ----*/

  addr_size = sizeof serverStorage;
  newSocket = accept(socket_desc, (struct sockaddr *) &serverStorage, &addr_size);
  puts("Connection is Accepted:");

  /*---- Send message to the socket of the incoming connection ----*/

   FILE *fp;
   FILE *recvfp;   
   int size=0,read_size=0,tot_size=0; 
   int i=0,stat=0,write_size=0,file_size=1;
   char file_buffer[1024],received_int[10],clientSocket;
   fp = fopen("/home/sosdt009/Desktop/character2.txt","r");
   recvfp = fopen("/home/sosdt009/Desktop/Rrec.txt","w");
   fseek(fp, 0, SEEK_END); // seek to end of file
   size = ftell(fp); // get current file pointer   
   while (size>tot_size)
   {
 		//Read from the file into our send buffer
		printf("Ready for sending \n");
		read_size = fread(buffer,sizeof(buffer),1,fp);	
		printf("Read size value is %d: \n",read_size);	
		//printf(" Multiple loop \n");		
		stat = send(clientSocket,buffer,read_size, 0);
		while (stat < 0);
                tot_size=tot_size+read_size;   		  
   }
    while(file_size>0)
   {
     printf("Receiving: \n");
     file_size=recv(clientSocket,file_buffer,sizeof(file_buffer), 0);
     printf("%d",file_size);
     write_size = fwrite(file_buffer,1,file_size,recvfp); 
     printf("bytes=%d",write_size);
   }
  shutdown(clientSocket,1);
  fclose(fp);
  fclose(recvfp);
  close(clientSocket);
  //printf("read size value is %d: \n",read_size);	
  //printf("size=%d",size);
  return 0;
}





和客户代码如下







and the client code is as follows


/****************** CLIENT CODE ****************/

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <netinet/in.h>

void main()
{
  int ClientSocket;
  struct sockaddr_in serverAddr;
  socklen_t addr_size;

  /*---- Create the socket. The three arguments are: ----*/
  /* 1) Internet domain 2) Stream socket 3) Default protocol (TCP in this case) */

  ClientSocket = socket(AF_INET, SOCK_STREAM, 0);
  
  /*---- Configure settings of the server address struct ----*/
  /* Address family = Internet */

  serverAddr.sin_family = AF_INET;

  /* Set port number, using htons function to use proper byte order */

  serverAddr.sin_port = htons(6777);

  /* Set IP address to localhost */

  serverAddr.sin_addr.s_addr = inet_addr("127.0.0.1");

  /* Set all bits of the padding field to 0 */

  memset(serverAddr.sin_zero, '\0', sizeof serverAddr.sin_zero);  

  /*---- Connect the socket to the server using the address struct ----*/

  addr_size = sizeof serverAddr;
  if(connect(ClientSocket, (struct sockaddr *) &serverAddr, addr_size)<0)
  {
	puts("Connection failed:");
	exit(0);
  }
  else
  {
	puts("Connection open for data:");
  }  
   FILE *fp;
   FILE *recvfp;   

   int size=0,read_size=0,tot_size=0; 
   int i=0,stat=0,write_size=0,file_size=1;
   char buffer[1024],file_buffer[1024],received_int[10];

   fp = fopen("/home/sosdt009/Desktop/character.txt", "r");
   recvfp = fopen("/home/sosdt009/Desktop/character.txt", "w");

   //fseek(fp, 0, SEEK_END); // seek to end of file
   //size = ftell(fp); // get current file pointer    
    
  /* sprintf(received_int, "%ld", size);
    printf("Sent int=%s",received_int);*/
  /*  send(clientSocket,received_int,sizeof(received_int),0);*/
    
  /* strcpy(received_int,"empty\n");
    recv(clientSocket, received_int,sizeof(received_int), 0);*/

  //write(clientSocket,&size, sizeof(size));
  // read(clientSocket, &received_int, sizeof(received_int));
  
  //recv(clientSocket, received_int,sizeof(received_int), 0);
  //printf("Received int=%s",received_int); 
  // fseek(fp, 0, SEEK_SET);
  // fread(buffer,size, 1, fp);   

  while(file_size>0)
   {
     printf("Receiving: \n");
     file_size=recv(ClientSocket,file_buffer,sizeof(file_buffer), 0);
     printf("%d",file_size);
     write_size = fwrite(file_buffer,1,file_size,recvfp); 
     printf("bytes=%d",write_size);
   }
  while (size>tot_size)
  { 
		//Read from the file into our send buffer
		//printf("Ready for sending \n");
		read_size = fread(file_buffer,1,sizeof(file_buffer),fp);	
		printf("read size value is %d: \n",read_size);	
		do
		{
			printf(" Multiple loop \n");		
			stat = send(ClientSocket,buffer,read_size, 0);
		} while (stat < 0);
                tot_size=tot_size+read_size;    		  
  }
  shutdown(ClientSocket,1);
  fclose(fp);
  printf("read size value is %d: \n",read_size);	
  printf("size=%d",size);
 /* while(file_size>0)
  {
     printf("Receiving: \n");
     file_size=recv(clientSocket,file_buffer,sizeof(file_buffer), 0);
     printf("%d",file_size);
     write_size = fwrite(file_buffer,1,file_size,recvfp); 
     printf("bytes=%d",write_size);
  }*/

  fclose(recvfp);
  close(ClientSocket);
/*Complete file block*/

//printf("%ld,%s\n", size,buffer);
//send(clientSocket,buffer,size,0);
//strcpy(buffer,"empty\n");
//recv(clientSocket, buffer, 1024, 0);

  /*---- Print the received message ----*/
//printf("Data received: %s",buffer);   
}

推荐答案

stat = send(clientSocket,buffer,read_size, 0);
while (stat < 0);
        tot_size=tot_size+read_size;



当返回值为-1时,调用失败(根据上述第一个错误应该在此处)。



虽然程序无法正常工作,但应检查所有函数返回值并在失败时打印它们。所以你知道哪个程序步骤失败了。你已经为一些电话做了这个。所以将输出添加到您的问题中。这使我们更容易提供帮助。


When the return value is -1, the call failed (and should be here according to the first error described above).

While your program does not work as intended you should check all function return values and print them upon failure. So you know which call fails at which program step. You did this already for some calls. So add the output to your question. That makes it much easier for us to help.


这篇关于如何使用tcp / ip协议双向双向发送和接收文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-21 19:16