LinuxQuestions.org
LinuxAnswers - the LQ Linux tutorial section.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Networking
User Name
Password
Linux - Networking This forum is for any issue related to networks or networking.
Routing, network cards, OSI, etc. Anything is fair game.

Notices

Reply
 
Search this Thread
Old 11-15-2005, 01:58 PM   #1
jacques83
Member
 
Registered: Nov 2005
Posts: 34

Rep: Reputation: 15
cannot read data at server socket, though client socket sends it


hello,

i am trying to read data at the server as it is sent from the client. the client side sends data, but server is not able to read it.please comment....

/*
** server.c -- a stream socket server demo
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/wait.h>
#include <signal.h>

#define MYPORT 3495 // the port users will be connecting to
#define BACKLOG 10 // how many pending connections queue will hold
#define MAXDATASIZE 100

struct IPsecAH
{
unsigned char next_header;
unsigned char payload_len;
unsigned short int reserved;
unsigned long int SPI;
unsigned long int seq_no;
unsigned char auth_data[16];

};

struct IPhdr
{
unsigned char ver_hlen;
unsigned char tos;
unsigned short int tlength;
unsigned short int identification;
unsigned short int flags_foff;
unsigned char ttl;
unsigned char protocol;
unsigned short int h_chksum;
unsigned long sourceip;
unsigned long destip;
};

struct databuf
{
unsigned char buf[4];
};

struct datagram
{
struct IPhdr ip;
struct IPsecAH ipsec;
struct databuf d;
};

void sigchld_handler(int s)
{
while(waitpid(-1, NULL, WNOHANG) > 0);
}

int main(void)
{
struct datagram d2;
int sockfd, new_fd; // listen on sock_fd, new connection on new_fd
struct sockaddr_in my_addr; // my address information
struct sockaddr_in their_addr; // connector’s address information
socklen_t sin_size;
struct sigaction sa;
int yes=1;
int n;
char recvbuf[MAXDATASIZE];

if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1)
{
perror("socket");
exit(1);
}

if (setsockopt(sockfd,SOL_SOCKET,SO_REUSEADDR,&yes,sizeof(int)) == -1)
{
perror("setsockopt");
exit(1);
}

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); // zero the rest of the struct

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

if (listen(sockfd, BACKLOG) == -1)
{
perror("listen");
exit(1);
}

sa.sa_handler = sigchld_handler; // reap all dead processes
sigemptyset(&sa.sa_mask);
sa.sa_flags = SA_RESTART;

if (sigaction(SIGCHLD, &sa, NULL) == -1)
{
perror("sigaction");
exit(1);
}

while(1)
{ // main accept() loop
sin_size = sizeof(struct sockaddr_in);
if ((new_fd = accept(sockfd, (struct sockaddr *)&their_addr,
&sin_size)) == -1)
{
perror("accept");
continue;
}

printf("server: got connection from %s\n",
inet_ntoa(their_addr.sin_addr));

if (!fork())
{ // this is the child process
close(sockfd); // child doesn't need the listener
if (send(new_fd, "Hello, world!\n", 14, 0) == -1)
perror("send");
if(n=recv(new_fd,(struct datagram *)&d2,sizeof(struct datagram),0)==-1)
perror("recv");
/*
recvbuf[n]='\0';
printf("%s",recvbuf);*/
printf("No of bytes recd: %d",n);
printf("IPsec Header:\n");
printf("\tNext header: %c\n",d2.ipsec.next_header);
printf("\tPayload_len: %c\n",d2.ipsec.payload_len);
printf("\treserved: %d\n",d2.ipsec.reserved);
printf("\tSPI: %ld\n",d2.ipsec.SPI);
printf("\tSequence number: %ld\n",d2.ipsec.seq_no);
printf("\tAuthentication data: %s\n", d2.ipsec.auth_data);

printf("\nIP header :");
printf("\n\tVersion: %02x", d2.ip.ver_hlen);
printf("\n\tTOS: %02x", d2.ip.tos);
printf("\n\tTotal length: %d", d2.ip.tlength);
printf("\n\tIdentification:%d", d2.ip.identification);
printf("\n\tFlags: %d", d2.ip.flags_foff);
printf("\n\tTTL: %02x", d2.ip.ttl);
printf("\n\tProtocol: %02x", d2.ip.protocol);
printf("\n\tHeader checksum: %d", d2.ip.h_chksum);
printf("\n\tSource IP: %ld", d2.ip.sourceip);
printf("\n\tDestination IP: %s", d2.d.destip);
printf("\nData: %s",d2.d.buf);


if(n==-1)
{
perror("recv");
printf("UNSUCCESSFULLLLLLLLLL");
}
// printf("N VALUE%d", n);
close(new_fd);
exit(0);
}

close(new_fd); // parent doesn't need this
}//end of while
// printf("%dN VALUE", n);
return 0;
}
 
  


Reply


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
Unable to connect to UNIX socket /tmp/.esd/socket error while using grip dr_zayus69 Linux - Software 4 08-23-2005 07:28 PM
how to implement fork() in socket programming b/w server/client in voice communicatio timeout4me Programming 1 08-05-2005 04:01 PM
creating server socket program to handle multiple client at same time cranium2004 Programming 2 03-14-2005 10:58 AM
read socket problem (proxy server related) jnusa Programming 9 11-25-2004 09:31 AM
compile the socket server for the client cool hat Linux - Software 0 06-19-2004 01:23 PM


All times are GMT -5. The time now is 11:43 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