Linux - Networking This forum is for any issue related to networks or networking.
Routing, network cards, OSI, etc. Anything is fair game. |
Notices |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
|
11-07-2014, 01:58 PM
|
#1
|
LQ Newbie
Registered: Nov 2014
Posts: 2
Rep:
|
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);
}
|
|
|
11-11-2014, 02:33 PM
|
#2
|
Senior Member
Registered: Apr 2009
Posts: 1,877
Rep:
|
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 */
|
|
|
|
All times are GMT -5. The time now is 04:45 AM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|