LinuxQuestions.org
View the Most Wanted LQ Wiki articles.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 05-11-2011, 11:30 AM   #1
shakthi
LQ Newbie
 
Registered: May 2011
Posts: 11

Rep: Reputation: 0
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...

Last edited by XavierP; 05-11-2011 at 02:51 PM. Reason: Code tags and moved to Programming
 
Old 05-11-2011, 09:56 PM   #2
plpl303a
Member
 
Registered: May 2011
Posts: 52

Rep: Reputation: 3
You should fclose() the file after you are done; otherwise, buffered data won't be saved.
 
Old 05-11-2011, 10:02 PM   #3
dwhitney67
Senior Member
 
Registered: Jun 2006
Location: Maryland
Distribution: Kubuntu, Fedora, RHEL
Posts: 1,523

Rep: Reputation: 332Reputation: 332Reputation: 332Reputation: 332
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;
}

Last edited by dwhitney67; 05-11-2011 at 10:04 PM.
 
  


Reply

Tags
server, socket


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Socket programming problem. sam1201 Programming 4 08-26-2009 12:40 AM
I/O problem in socket programming goldeneagle1234 Programming 2 06-19-2009 12:06 PM
I/O problem in socket programming goldeneagle1234 Linux - Networking 1 06-19-2009 11:24 AM
Socket Programming problem lucky6969b Programming 4 12-27-2005 12:44 AM
socket programming problem bgraur Programming 2 03-09-2003 09:05 AM


All times are GMT -5. The time now is 07:35 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration