Error in send and recv(). Please help.



I wrote a client and server program in C. The server is actually IPv6
socket and client is IPv4. They both use the storage structure
sockaddr_storage to cast to IPv6 and IPv4.

The client and server are connected but the server cannot send the
data to the client. I am pasting the code. Do you see any obvious
mistake in the code.

Please help. Every help is greatly appreciated.

Thanks.


I compiled the code using gcc -client.c -o client and gcc server.c -o
server, and then running the ./server and ./client 127.0.0.1.

***********Server Code*******************
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <netdb.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>

#define PORT 3500
#define LISTEN_COUNT 5
#define INET_ADDRSTRLEN 16
#define INET6_ADDRSTRLEN 46


int main()
{
int sockfd, new_fd;

char str[INET6_ADDRSTRLEN];

/* Storage struct */
struct sockaddr_storage my_addr, their_addr;

/* IPv6 struct */
struct sockaddr_in6 *in6;
struct sockaddr_in6 *in6their;

socklen_t my_addrlen = sizeof(my_addr);
socklen_t their_addrlen = sizeof(their_addr);

printf("\n Length of my_addr = %d\n", my_addrlen);

sockfd = socket(AF_INET6, SOCK_STREAM, 0);
if (sockfd == -1){
printf("\n Error creating socket");
exit(1);
}

/* Casting Storage to IPv6 */
in6 = (struct sockaddr_in6 *) &my_addr;

/* Populating Values */
// (*in6).sin6_len = sizeof(struct sockaddr_in6);
(*in6).sin6_family = AF_INET6;
(*in6).sin6_port = htons(PORT);
(*in6).sin6_addr = in6addr_any; //loopback;

/* binding */
if ( (bind(sockfd, (struct sockaddr *) &my_addr, my_addrlen) == -1) )
{
printf("\n Can't bind");
exit(1);
}

/* Listening */
if (listen(sockfd, LISTEN_COUNT) == -1) {
printf("\n Error( listening");
exit(1);
}

printf("\n Server: I am listening\n");

/* Accept, Print and Send back through another child process */
while (1){
if ( (new_fd = accept(sockfd, (struct sockaddr *) &their_addr,
&their_addrlen) == -1) ){
printf("\n Can't accept");
continue;
}
/* Casting Storage to IPv4 */
in6their = (struct sockadd_in6 *) &their_addr;


printf("\n Server got connection from %s\n", inet_ntop(AF_INET6,
&( (*in6their).sin6_addr), str, sizeof(str) ) );

if (!fork()) { // this is the child process
close(sockfd); // child doesn't need the listener
if (send(new_fd, "Hello World", sizeof("Hello World"), 0) == -1) {
printf("Error sending");
close(new_fd);
exit(0);
}
}

close(new_fd); // parent doesn't need this
}

return 0;
}


***********Client Code*******************

#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <netdb.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>

#define PORT 3500
#define MAXDATASIZE 100

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

int sockfd, numbytes;
char buf[MAXDATASIZE];

struct sockaddr_storage my_addr;
struct hostent *he;

struct sockaddr_in *in4;

if (argc != 2){
printf("\n Incorrect usage\n");
}


if ( (he=gethostbyname2(argv[1], AF_INET) ) == NULL ) {
printf("\n Error in gethostbyname\n");
exit(1);
}

sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd == -1){
printf("\n Error creating socket\n");
exit(1);
}

in4 = (struct sockaddr_in *) &my_addr;

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

memset((*in4).sin_zero, '\0', sizeof((*in4).sin_zero) );

if ( connect(sockfd, (struct sockaddr *)&my_addr, sizeof(my_addr)) ==
-1) {
printf("\n Error Connecting\n");
exit(1);
}

printf("\n Client: I have been connected\n");

if ((numbytes=recv(sockfd, buf, MAXDATASIZE-1,0)) == -1) {
printf("\n Error recieving\n");
exit(1);
}

printf("\n Client: I have received data\n");


buf[numbytes] = '\0';
printf("\n Received: %s\n", buf);

close(sockfd);

return 0;

}

.



Relevant Pages

  • Re: Socket Programming Problem
    ... number to the server and server increments the number and sends it back to ... the client. ... exit(EXIT_FAILURE); ... if(bind(sockfd, (struct sockaddr*) &server_str, ...
    (comp.unix.programmer)
  • Re: Error in send and recv(). Please help.
    ... The client and server are connected but the server cannot send the ... struct sockaddr_in6 *in6their; ... int sockfd, numbytes; ...
    (comp.unix.programmer)
  • Socket Programming in C
    ... I'm new to socket programming and what I am trying to do is create ... a client and server program that simulates rolling a die. ... struct sockaddr_in ServAddr, ClntAddr; ...
    (comp.unix.programmer)
  • GetQueuedCompletionStatus() never returns
    ... - an OVERLAPPED struct ... - lpParameter as a pointer to the "server" struct ... - grab an unused "client" struct from the pool ... - CompletionPort as the "server" completion port ...
    (microsoft.public.win32.programmer.networks)
  • How can I run server and client applications in two different computers?
    ... I found some example code about server and client applications. ... int main{ ... struct sigaction sa; ...
    (comp.os.linux.development.apps)