shrutijadhav 0 Newbie Poster

i am trying socket programming in linux(fedora 7) using C language.
i want to send a buffer from server to client,and vice versa.
but the contents are not getting saved.the contents are unsigned long so i am converting them into ascii usinf sprintf and then saving in buffer.but its not getting saved.it shows that buffer is empty.please help me.

my attachin code.

SERVER SIDE:

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<netdb.h>


#define MYPORT 3490



int main()
{
int result,result1;
int sockfd;


struct sockaddr_in my_addr;


char buffer[40];
char req_buffer[40];


sockfd = socket(AF_INET, SOCK_STREAM, 0);
if(sockfd < 0)
{
printf("Creating socket error\n");
//return -1;
}
printf("\n socket created\n");


my_addr.sin_family = AF_INET;          // host byte order
my_addr.sin_port = htons(MYPORT);      // short, network byte order
my_addr.sin_addr.s_addr = INADDR_ANY;  // automatically fill with my IP
memset(&(my_addr.sin_zero), '\0', 8);


printf("\nsdsd\n");



if (bind(sockfd, (struct sockaddr *)&my_addr, sizeof(struct sockaddr))== -1)
{
perror("bind");
exit(1);
}
printf("\n bind successfull\n");


if (listen(sockfd,15) == -1)
{
perror("listen");
exit(1);
}
printf("\n listen successfull\n");


struct sockaddr_in hisaddr;
int acceptfd;
socklen_t addrlen;



while (1)
{


acceptfd = accept(sockfd,(struct sockaddr *)&hisaddr, &addrlen);
if(acceptfd < 0)
{
printf("Error accepting on the socket %d\n");
continue;
}
printf("%s: Accepted Connection\n");


unsigned long p,g,SA,SB,TA,TB;


p=32;
sprintf(buffer[0],"%d",p);


q=4
sprintf(buffer[1],"%d",g);


TA=100;
sprintf(buffer[2],"%d",TA);



result1 = send(acceptfd, buffer,14, 0);



if(result<0)
{
printf("\n error while sending from serv-cli");
}
printf("\n send success from serv-cli");*/


result1 = recv(acceptfd, req_buffer, 1024, 0);
if(result1 < 0)
{
printf("Error reading on the socket %d\n");


continue;
}
req_buffer[result1] = '\0';
printf("\nReceived: %s\n",req_buffer);


close(acceptfd);


}
close(sockfd);
return 0;
}

CILENT SIDE

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<netdb.h>


#define SERV_PORT 3490



int main(int argc,char *argv[])
{
int servsock,result,receive;
char buffer[40];


struct sockaddr_in servaddr;
struct hostent *he;



if (argc != 2)
{
fprintf(stderr,"usage: client hostname\n");
exit(1);
}
if ((he=gethostbyname(argv[1])) == NULL) // get the host info
{
herror("gethostbyname");
exit(1);
}


servsock = socket(AF_INET, SOCK_STREAM, 0);
if(servsock < 0)
{
printf("Error creating socket connecting to server\n");


}


servaddr.sin_family = AF_INET;
servaddr.sin_addr = *((struct in_addr *)he->h_addr);
servaddr.sin_port = htons(SERV_PORT);


memset(&(servaddr.sin_zero), '\0',8);


result = connect(servsock, (struct sockaddr *)&servaddr, sizeof(servaddr));
if(result < 0) {
printf("Error connecting to server \n");
}



char buf[40];
unsigned long p,g,SA,SB,TA,TB;
int numbytes,i=0;
while(1)
{
if ((numbytes=recv(servsock, buf, 14, 0)) == -1)
{
perror("recv");
exit(1);
}
buf[numbytes] = '\0';


printf("\n Received: %s\n",buf);
printf("numbytes= %d",numbytes);



sscanf(buf[0],"%ld",&p);
sscanf(buf[1],"%ld",&g);
sscanf(buf[2],"%ld",&TA);



printf("p: %d\n g: %d\n TA: %d\n",p,g,TA);



printf("enter data to send");
scanf("%s",buffer);
result = send(servsock, buffer, 14, 0);
if(result<0)
{
printf("\n error while sending from cli-serv");
}
printf("\n send success from cli-serv");


}
close(servsock);


return 0;
}