LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
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 07-03-2009, 07:24 AM   #1
naveenisback
Member
 
Registered: Jun 2009
Posts: 80
Blog Entries: 1

Rep: Reputation: 16
not getting complete message while communicating in socket programming


Hi frenz,


I wrote a client program in redflag in C language. I have to communicate with the server which is running in windows platform whose IP is 192.168.1.8. after executing the program sending message to server is success, but i am unable to receive complete message. my client program is as follows. pls find the bug in it.

#include <stdio.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <netinet/in.h>
#include <sys/time.h>
#include <time.h>
#include <fcntl.h>
#include <malloc.h>
#define BUFFSIZE 5000
#define NO_MESSAGES 18




char* send_buffer[NO_MESSAGES]= {"<?xml version ='1.0'?><Vigyan><message>Systeminfo</message></Vigyan>","<?xml version='1.0'
?><Vigyan><message>Setpowersettings</message></Vigyan>","<?xml version='1.0' ?><Vigyan><message>BatteryInformation</message>
</Vigyan>","<?xml version ='1.0'?><Vigyan><message>Currenthostsettings</message></Vigyan>","<?xml version ='1.0'?><Vigyan><m
essage>PowerSchemes</message></Vigyan>","<?xml version ='1.0'?><Vigyan><message>Currentpowersettings</message></Vigyan>","<?
xml version='1.0' ?><Vigyan><message>Getsystemstatus</message></Vigyan>","<?xml version='1.0' ?><Vigyan><message>DeleteSchem
e</message></Vigyan>","<?xml version='1.0' ?><Vigyan><message>CreateScheme</message></Vigyan>","<?xml version ='1.0'?><Vigya
n><message>Hibernate</message></Vigyan>","<?xml version ='1.0'?><Vigyan><message>ShutDown</message></Vigyan>","<?xml version
='1.0'?><Vigyan><message>StandBy</message></Vigyan>","<?xml version ='1.0'?><Vigyan><message>Restart</message></Vigyan>","<
?xml version ='1.0'?><Vigyan><message>Logoff</message></Vigyan>","<?xml version ='1.0'?><Vigyan><message>Pause</message></Vi
gyan>","<?xml version ='1.0'?><Vigyan><message>PauseStatus</message></Vigyan>","<?xml version ='1.0'?><Vigyan><message>Resum
e</message></Vigyan>","<?xml version ='1.0'?><Vigyan><message>querysettings</message></Vigyan>"};


void Die(char *mess)
{
perror(mess);
}

char* timestamp()
{

struct tm* current;

time_t ltime;

char* time_now;

ltime = time(NULL);

time_now = asctime(localtime(&ltime));

return time_now;

}

int main(int argc, char *argv[])
{

int sock,i;

FILE *fptr;

int length[NO_MESSAGES];

long h_length[NO_MESSAGES];

struct sockaddr_in server_addr;


char receive_buffer[BUFFSIZE];
int echolen;

int received = 0;

if (argc !=3 )
{

printf("Give IP and PORT as command line arguments \n");

exit(1);

}
//cteate a socket
if ((sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0)
{

Die("Failed to create socket");
exit(1);
}


memset(&server_addr, 0, sizeof(server_addr));

server_addr.sin_family = AF_INET;

server_addr.sin_addr.s_addr = inet_addr(argv[1]);

server_addr.sin_port = htons(atoi(argv[2]));


if (connect(sock,(struct sockaddr *) &server_addr,sizeof(server_addr)) < 0)
{

Die("Failed to connect with server");
}

fptr = fopen("log.txt","w+");

if(fptr == NULL)
{

perror("fopen:");

exit(1);

}


fwrite("\tLOG FILE\n\nTIME\t\t\t\tMESSAGE RECEIVED\n", 36, 1, fptr);


int bytes = 0;
long receivedlength =0;
long h_receivelen;

for(i = 0; i < NO_MESSAGES; i++)
{
length[i] = strlen(send_buffer[i]);

h_length[i]= htonl(length[i]);

send(sock, (const char*)&h_length[i], 4, 0);

if (send(sock, send_buffer[i], length[i], 0) != length[i])
{

Die("Mismatch in number of sent bytes");
exit(1);
}
memset(receive_buffer, 0, sizeof(receive_buffer));

recv(sock, (char*)&receivedlength, 4, 0);

h_receivelen = ntohl(receivedlength);

char* recvbufptr = receive_buffer;

while(1)
{

bytes = recv(sock, recvbufptr, h_receivelen, 0);

char* time_now = timestamp();

printf("THE TIME IS >>>>>>%s\n", time_now);

fprintf(fptr,"%s\t%s\n",time_now ,send_buffer[i]);

if((bytes == 0) || (bytes == -1))
{
perror("recv:");
break;
}
if (bytes == h_receivelen)
{
break;
}
else
{
h_receivelen = h_receivelen - bytes;
recvbufptr = recvbufptr + bytes;
break;
}
}

receive_buffer[bytes] = '\0';

printf("\nRECIEVE BUFFER IS :%s\n", receive_buffer);


}//for

fseek(fptr, 0, SEEK_END);

int size_file = ftell(fptr);

rewind(fptr);

char* str = (char*) malloc (sizeof(str));

if(str == NULL)
{
perror("malloc:");
exit(1);

}

int bytes_file = fread(str,1, size_file, fptr);

printf("%s%d\n", str,bytes_file);

fclose(fptr);

close(sock);

// free(str);
return 0;
} // main


output is

THE TIME IS >>>>>>Fri Jul 3 17:50:01 2009

recv:: Success

RECIEVE BUFFER IS :<


THE TIME IS >>>>>>Fri Jul 3 17:50:01 2009

recv:: illegal seek

RECIEVE BUFFER IS :s


require output is some xml messages of very large bytes, which wass stored in char array buffer in windows. this is iron core problem to my HR also.

pls if this info is not enough contact me at hellohinaveen@gmail.com


one more information. It is exactly working when i communicate with the server wich is runnning in my system itself. got this point. sorry im weak in english.

Last edited by naveenisback; 07-03-2009 at 07:28 AM. Reason: more information
 
Old 07-03-2009, 07:53 AM   #2
Telemachos
Member
 
Registered: May 2007
Distribution: Debian
Posts: 754

Rep: Reputation: 60
Please put code into [code][/code] tags and indent it.
 
Old 07-05-2009, 08:29 AM   #3
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
you need to make it easy for us.
Sorry, but nobody is going to be bothered to try and read such a mess.
remember recv only reads as much as the buffer size you specify.
you will need to loop around.

also a Win machine may be using \r\n line endings.
 
  


Reply



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 programing - using message boundaries libinmeledam Linux - Networking 4 06-03-2008 06:48 PM
message size TCP socket ibanezul Linux - Networking 2 10-23-2007 04:07 PM
Programming C++ in emacs - auto complete xeon123 Programming 1 04-29-2007 06:40 PM
Multiple clients communicating to one socket dragondad Programming 3 09-10-2005 08:22 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:32 PM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration