Click here to Skip to main content
15,868,016 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Client code:


C++
  int main(void)
  {
  int sockfd = 0,n = 0;
  char recvBuff[1024];
  struct sockaddr_in serv_addr;
  memset(recvBuff, '0' ,sizeof(recvBuff));

  /* create a socket first */

  if((sockfd = socket(PF_INET, SOCK_STREAM, 0))< 0)
  {
  printf("\n Error : Could not create socket \n");
  return 1;
  }

  /* Initialize sockaddr_in data structure */

 serv_addr.sin_family = PF_INET;
 serv_addr.sin_port = htons(5000);
 serv_addr.sin_addr.s_addr = inet_addr("127.0.0.1");

    /* Attempt a connection */

  if(connect(sockfd, (struct sockaddr *)&serv_addr, sizeof(serv_addr))<0)
   {
    printf("\n Error : Connect Failed \n");
   return 1;
   }

 /* Create file where data will be stored */
 FILE *fp;
 fp = fopen("document.txt", "r");
 if(NULL == fp)
 {
    printf("Error opening file");
    return 1;
 }


  /* Receive data in chunks of 1024 bytes */

    while((n = read(sockfd, recvBuff, sizeof(recvBuff)-1)) > 0)
  {
   printf("\n \n Bytes received %d \n , n");
   // recvBuff[n] = 0;
  if(fputs(recvBuff, stdout) == EOF)
  {
  printf("\n Error : Fputs error");
  printf("\n %s",recvBuff);
  }

  if( n &lt; 0)
  {
  printf("\n Read Error \n");
  }


 return 0;
  }
}




Server code:


C++
int main(void)

{
int sockfd = 0,n = 0;

char recvBuff[1024];

struct sockaddr_in serv_addr;

memset(recvBuff, '0' ,sizeof(recvBuff));

/* create a socket first */

 if((sockfd = socket(PF_INET, SOCK_STREAM, 0))< 0)
{
 printf("\n Error : Could not create socket \n");
 return 1;
 }

/* Initialize sockaddr_in data structure */

 serv_addr.sin_family = PF_INET;
serv_addr.sin_port = htons(5000);
serv_addr.sin_addr.s_addr = inet_addr("127.0.0.1");

/* Attempt a connection */

if(connect(sockfd, (struct sockaddr *)&serv_addr, sizeof(serv_addr))<0)
{
 printf("\n Error : Connect Failed \n");
 return 1;
 }

/* Create file where data will be stored */
 FILE *fp;
 fp = fopen("document.txt", "r");
 if(NULL == fp)
 {
  printf("Error opening file");
  return 1;
  }


  /* Receive data in chunks of 1024 bytes */

  while((n = read(sockfd, recvBuff, sizeof(recvBuff)-1)) > 0)
 {
  printf("\n \n Bytes received %d \n , n");
  // recvBuff[n] = 0;
  if(fputs(recvBuff, stdout) == EOF)
  {
  printf("\n Error : Fputs error");
  printf("\n %s",recvBuff);
  }

 if( n &lt; 0)`
 {
  printf("\n Read Error \n");
 }


 return 0;
   }
  }


I have to send the data from server and client respectively,
and at the same time output should be displayed on the both
side simultaneously. What is wrong with this code? Please help me
through this.
Posted
Updated 13-Mar-15 19:48pm
v4
Comments
Mike Nordell 14-Mar-15 2:02am    
It seems you are coming from a *nix background. You are very close, I think,.

read() is not the universal "read" function in Windows, and an "fd" is not the universal file descriptor.

I urge you to read the documentation at https://msdn.microsoft.com/en-us/library/windows/desktop/ms740642%28v=vs.85%29.aspx

(wicked URL, but it should take you to "What's New for Windows Sockets", and the vast majority of those functions have been there since NT4 (and earlier).

++luck;
[no name] 14-Mar-15 2:15am    
To add to the above: I don't see any sends(). Sending and receiving in the same thread ain't going to work. Why not look for some working code - there's plenty on this site.
Sergey Alexandrovich Kryukov 14-Mar-15 17:42pm    
"Simultaneously"?! What, you don't know special theory of relativity?
—SA

1 solution

As outlined above, by other members, you seems to have a lot of good will, but some background holes :)
Let think about what you want achive:
1. A client try to connect to a server to achieve data
2. A server should be Listening for a connection
3. The server see a connection request and accept it
4. Both partners send and receive data writing and reading from the connected socket
Now the points 2 and 3 are missing from your code.
Both, client and server, try to connect to something, but no code to accept the connection is present.
See[^] this code as starting point.
And, as usual, good luck ;)
 
Share this answer
 
v4
Comments
Albert Holguin 15-Mar-15 23:11pm    
+5

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900