LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   hi please help me - problem in socket programming (http://www.linuxquestions.org/questions/programming-9/hi-please-help-me-problem-in-socket-programming-880122/)

shakthi 05-11-2011 11:30 AM

hi please help me - problem in socket programming
 
hi, anyone please help me as possible...

i have problem in socket programming, while displaying received message in file,i got a problem... i cant able to write it in the file....

this is the code....

Code:

/* tcpserver.c */

#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>

Pthread_t thread1;
int main( )
{
        int sock, connected, bytes_recieved , true = 1; 
        char send_data [1024] , recv_data[1024];     

        struct sockaddr_in server_addr,client_addr;   
        int sin_size;
 
        if ((sock = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
            perror("Socket");
            exit(1);
        }

     
       
        server_addr.sin_family = AF_INET;       
        server_addr.sin_port = htons(5000);   
        server_addr.sin_addr.s_addr =htonl( INADDR_ANY);
        bzero(&(server_addr.sin_zero),8);

        if (bind(sock, (struct sockaddr *)&server_addr, sizeof(server_addr))
                                                                      == -1) {
            perror("Unable to bind");
            exit(1);
        }

        if (listen(sock, 5) == -1) {
            perror("Listen");
            exit(1);
        }
               
        printf("\nTCPServer Waiting for client on port 5000");
     

sin_size = sizeof(client_addr);
        while(1)
        { 

            Printf(“\n waiting for connection”);

            if((connected = accept(sock, (struct sockaddr *)&client_addr,&sin_size))!=-1)
{

            printf("\n I got a connection from (%s , %d)",
                  inet_ntoa(client_addr.sin_addr),ntohs(client_addr.sin_port));

            pthread_create(&thread1,NULL,(void*)&func1,(int*)&connected);
pthread_detach(thread1);
}
Else
{
Printf(“\n error in connection”);
}
}
Close(sock);
Return 0;
}
Void *func1(int *temp)
{
Char msg[1000],msg1[1000];
FILE *fp;
Fp=fopen(“log.txt”,”w”);
While(1)
{
Recv(*temp,msg,sizeof(msg),0);
Printf(“received msg is %s”,msg);
Fprintf(fp,”%s”,msg);
}
}


now my problem is run time error i can able to create file but i cant able to write file....log.txt contain nothing.... as here i have give sample code... dont say not initialising function and all.... i have initialised , please only see func1() - my problem is only not able to write msg which i got received from the client.... please help me as soon as possible...

plpl303a 05-11-2011 09:56 PM

You should fclose() the file after you are done; otherwise, buffered data won't be saved.

dwhitney67 05-11-2011 10:02 PM

Because of the initial capitalization of the keywords in your code towards the end of the main() function and the thread function, the code you posted will not compile. Perhaps an error in posting your code?

As for the run-time problem, it appears to be in your thread function; you need to consider the return value from recv() such that the appropriate action may be performed. For example:
Code:

void *func1(int *client)
{
  char msg[1024];
  FILE *fp = fopen(“log.txt”,”w”);

  while(1)
  {
      int bytesRcvd = recv(*client, msg, sizeof(msg) - 1, 0);

      if (bytesRcvd > 0)
      {
        msg[bytesRcvd] = '\0';

        printf(“received msg is %s”, msg);

        fprintf(fp, ”%s”, msg);
      }
      else if (bytesRcvd == 0)
      {
        printf("client (%d) disconnected.\n", *client);
        break;
      }
      else
      {
        if (errno == EAGAIN || errno == EINTR)
            continue;

        printf("Error servicing socket for client %d.\n", *client);
        break;

      }
  }

  close(*client);

  return NULL;
}



All times are GMT -5. The time now is 05:04 AM.