ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
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
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:
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.