Member
Registered: Nov 2005
Posts: 34
Rep:
|
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;
}
|