LinuxQuestions.org
Visit Jeremy's Blog.
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-07-2014, 01:58 PM   #1
godna
LQ Newbie
 
Registered: Nov 2014
Posts: 2

Rep: Reputation: Disabled
Socket dual client/server Linux


I'm trying to make a "dual/server client" (ipv4,ipv6) with sockets in linux but i don't know how to join both codes. I have a dual client ipv4 and ipv6, but i have problems with the server if you notice the only difference between them it's the AF_INET (pf_inet ipv4, and if_inet6 ipv6) and the port of the server (1234 ipv4, 5678 ipv6). I need help how i can do a program that can connect hear ipv4 and ipv6 in the same port at the same time (dual stack)?

Ipv4 server:

Code:
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <unistd.h>
#include <netdb.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <string.h>
#include <strings.h>
#include <arpa/inet.h>

char *
addrtype(int addrtype)
{
switch(addrtype) {
case AF_INET:
return "AF_INET";
case AF_INET6:
return "AF_INET6";
}
return "Unknown";
}
int
abrir()
{
int escuchafd = 0;
struct sockaddr_in sin;
escuchafd = socket (AF_INET, // Internet IPv4
SOCK_STREAM, // TCP
0);
if (escuchafd == -1)
{
perror ("No se pudo abrir el socket");
exit(-1);
}
bzero(&sin, sizeof(sin));
sin.sin_family = AF_INET;
sin.sin_addr.s_addr = htonl(INADDR_ANY);
sin.sin_port = htons(1234);
if (bind(escuchafd, (struct sockaddr*)&sin, sizeof(sin)) ==-1)
{
perror("No se pudo hacer la asociación (bind)");
exit(-1);
}
return escuchafd;
}
void
print_addr(struct sockaddr_in *addr)
{
char host[512];
printf ("\tFamily:%s\n", addrtype(addr->sin_family));
printf ("\tPort: %d\n", ntohs(addr->sin_port));
printf ("\tIP Address: %s\n", inet_ntoa(addr->sin_addr));
int res = getnameinfo((const struct sockaddr *)(addr),
(socklen_t) sizeof(*addr),
host,
512,
NULL,
0, 0);
if (res == 0)
printf ("\tHostname: %s\n", host);
}
void
echo(int confd)
{
char msg[1024];
char snt[1024];
int res = 0;
while (res == 0)
{
bzero(msg, 1024);
if (read(confd, msg, 1024)>0)
printf ("CLI: %s", msg);
if (strncmp(msg, ".", 1) == 0)
res = 1;
else
{
strcpy(snt, "SRV: ");
strcat(snt, msg);
write(confd, snt, strlen(snt));
}
}
}
void
atender(int escuchafd)
{
struct sockaddr_in cli;
int confd;
socklen_t slen; // for (;;)
{
slen = sizeof(cli);
confd = accept(escuchafd, (struct sockaddr*)&cli, &slen);
if (confd == -1)
{
perror("No se pudo establecer la conexión (accept)");
exit(-1);
}
printf ("Conexión establecida: \n");
print_addr(&cli);
echo (confd);
}
}
int
main()
{
int escuchafd = 0;
escuchafd = abrir();
if (listen(escuchafd, 5) == -1)
{
perror ("No se pudo poner en escucha (listen)");
exit(-1);
}
atender(escuchafd);
close(escuchafd);
exit(0);
}
Ipv6 server:

Code:
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <unistd.h>
#include <netdb.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <string.h>
#include <strings.h>
#include <arpa/inet.h>

char *
addrtype(int addrtype)
{
switch(addrtype) {
case AF_INET:
return "AF_INET";
case AF_INET6:
return "AF_INET6";
}
return "Unknown";
}
int
abrir()
{
int escuchafd = 0;
struct sockaddr_in6 sin;
escuchafd = socket (AF_INET6, // Internet IPv6
SOCK_STREAM, // TCP
0);
if (escuchafd == -1)
{
perror ("No se pudo abrir el socket");
exit(-1);
}
bzero(&sin, sizeof(sin));
sin.sin6_family = AF_INET6;
sin.sin6_port = htons(5678);
sin.sin6_addr= in6addr_any;
sin.sin6_flowinfo = 0;
if (bind(escuchafd, (struct sockaddr*)&sin, sizeof(sin)) ==-1)
{
perror("No se pudo hacer la asociación (bind)");
exit(-1);
}
return escuchafd;
}
void
print_addr(struct sockaddr_in6 *addr)
{
char host[512];
bzero(host, 512);
inet_ntop(AF_INET6,
(const void*)&(addr->sin6_addr),
host,
512
);
printf ("\tFamily: %s\n", addrtype(addr->sin6_family));
printf ("\tPort: %d\n", ntohs(addr->sin6_port));
printf ("\tIP Address: %s\n", host);
bzero(host, 512);
int res = getnameinfo((const struct sockaddr *)(addr),
(socklen_t) sizeof(*addr),
host,
512,
NULL,
0, 0);
if (res == 0)
printf ("\tHostname: %s\n", host);
}
void
echo(int confd)
{
char msg[1024];
char snt[1024];
int res = 0;
while (res == 0)
{
bzero(msg, 1024);
if (read(confd, msg, 1024)>0)
printf ("CLI: %s", msg);
if (strncmp(msg, ".", 1) == 0)
res = 1;
else
{
strcpy(snt, "SRV: ");
strcat(snt, msg);
write(confd, snt, strlen(snt));
}
}
}
void
atender(int escuchafd)
{
struct sockaddr_in6 cli;
int confd;
socklen_t slen; // for (;;)
{
slen = sizeof(cli);
confd = accept(escuchafd, (struct sockaddr*)&cli, &slen);
if (confd == -1)
{
perror("No se pudo establecer la conexión (accept)");
exit(-1);
}
printf ("Conexión establecida: \n");
print_addr(&cli);
echo (confd);
}
}
int
main()
{
int escuchafd = 0;
escuchafd = abrir();
if (listen(escuchafd, 5) == -1)
{
perror ("No se pudo poner en escucha (listen)");
exit(-1);
}
atender(escuchafd);
close(escuchafd);
exit(0);
}
 
Old 11-11-2014, 02:33 PM   #2
nini09
Senior Member
 
Registered: Apr 2009
Posts: 1,877

Rep: Reputation: 162Reputation: 162
You can use the sockaddr_storage structure, defined in sys/socket.h, to write protocol-independent application programs. This data structure is large enough to accommodate both AF_INET and AF_INET6 protocol-specific address structures. It is also aligned at an appropriate boundary so that pointers to it can be cast as pointers to protocol specific address structures and they can be used to access the fields of those structures without alignment problems.

Server example code like following.
Quote:
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <stdio.h>

#define MAX_CONN 5

main(argc, argv)
int argc;
char *argv[];
{
struct addrinfo hints, *res, *ressave;
struct sockaddr_storage from;
int fromlen;
int err_num;
int oldsock, newsock;
int readval;
char buf[1024];
int conn_num = 0;

memset(&hints, 0, sizeof(hints));
hints.ai_family = AF_INET6;
hints.ai_flags = AI_PASSIVE | AI_ADDRCONFIG;
hints.ai_socktype = SOCK_STREAM;

err_num = getaddrinfo(NULL, argv[1], &hints, &res);
if(err_num) {
fprintf(stderr, "getaddrinfo: %s", gai_strerror(err_num));
exit(1);
}
ressave = res;
do {
oldsock = socket(res->ai_family, res->ai_socktype,
res->ai_protocol);
if(oldsock < 0)
continue;
if(bind(oldsock, res->ai_addr, res->ai_addrlen) < 0) {
perror("bind");
close(oldsock);
continue;
}
if(listen(oldsock, 5) < 0) {
perror("listen");
close(oldsock);
continue;
}
do {
if((newsock = accept(oldsock, (struct sockaddr *)&from,
&fromlen)) < 0) {
perror("accept");
close(oldsock);
}
else {
do {
memset(&buf, 0, sizeof(buf));
if((readval = read(newsock, buf, 1024)) == -1)
perror("read");
if(readval == 0)
printf("Ending connection\n");
else
printf("---> %s\n", buf);
} while (readval > 0);
conn_num++;
}
close(newsock);
} while (conn_num < MAX_CONN);
/* break after establishing the required no. of connections */
close(oldsock);
break;
} while ((res = res->ai_next) != NULL);
if(!res) {
fprintf(stderr, "bind/listen/accept failed for all addresses\n");
freeaddrinfo(ressave);
exit(1);
}
freeaddrinfo(ressave);
} /* end of main */
 
  


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
Tcp/ip socket server and client program - Redhat linux server 5(eclipse CDT) parvathi reddy Linux - Newbie 2 03-16-2014 02:51 AM
Client Server Socket TCP/IP program in C Linux over HTTPS fahad.anwar Linux - Newbie 6 05-29-2012 03:59 AM
Client Server Socket TCP/IP program in C Linux using threads fahad.anwar Linux - Newbie 2 05-18-2012 04:34 AM
linux server socket:client IE or firefox athena_1 Programming 3 08-17-2006 08:57 AM
cannot read data at server socket, though client socket sends it jacques83 Linux - Networking 0 11-15-2005 01:58 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Networking

All times are GMT -5. The time now is 04:45 AM.

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