Socket dual client/server Linux


 
Thread Tools Search this Thread
Top Forums Programming Socket dual client/server Linux
# 1  
Old 11-07-2014
Wrench 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);
}

# 2  
Old 11-07-2014
IPV4 clients and servers can talk to IPV6 clients and servers if they can resolve the address and port. So, an IPV4 server is already dual accessible but a high IPV6 server might be inaccessible to an IPV4 client. If the server is using inetd, it does not have to deal with IPV6/4. inetd code shows how to be a dual server.
# 3  
Old 11-07-2014
I know, the Ipv4 server only hear's Ipv4 clients and it's the same with ipv6 server that only can talk with ipv6 client's. Thats it's why I want more information or help to become the ipv6 server in a dual server, but i don't know how. But thanks I'm wonna read inetd code
# 4  
Old 11-09-2014
IPV6 clients can use low ip/port IPV4 servers, and IPV4 cients can use low ip/port IPV6 servers as long as there is a dual IP stack on the server and dual net fabric between. Remember that, on a client or server, a connection just devolves into an open socket fd. If I am listening on or connecting to port 80 TCP, I never know which stack my connection came from.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

C Socket Client/Server Fork Loop

Hello people. I'm trying to do something like a search engine. Server runs in the background by using ./server & which has data from a textfile stored in an array. Client then runs by using ./client It will then prompt "Search for:" For example, if I searched for Products called Instant... (0 Replies)
Discussion started by: andylbh
0 Replies

2. Programming

UDP linux client and Windows server

Hi, I have a situation where i need to communicate a linux client with a windows server, I am using a UDP socket communication channel. I am able to send packets from my linux clients to the windows server but unable to receive any data packet from the server. Do i need to make any setting in... (0 Replies)
Discussion started by: John20
0 Replies

3. Programming

socket programing in client server

hei, i want to enter name and read it by client server socket program after checking name validity put the password and check.if ok than server clirnt say correct other wise incorrect. below my code: char name; printf ("Welcome to the server \n"); printf ("Enter user name: \n"); scanf... (1 Reply)
Discussion started by: saiful_911
1 Replies

4. Programming

how can I send and receive data in client server socket programing

char name; printf ("Welcome to the server \n"); printf ("Enter user name: \n"); scanf ("%c", &name); how can client send name to server:what should be the code? int send ( int sid , const char ∗buffer Ptr , int len , int f l a g ) how can client receive ack from... (1 Reply)
Discussion started by: saiful_911
1 Replies

5. Programming

Client/Server Socket Application - Preventing Client from quitting on server crash

Problem - Linux Client/Server Socket Application: Preventing Client from quitting on server crash Hi, I am writing a Linux socket Server and Client using TCP protocol on Ubuntu 9.04 x64. I am having problem trying to implement a scenario where the client should keep running even when the... (2 Replies)
Discussion started by: varun.nagpaal
2 Replies

6. UNIX for Dummies Questions & Answers

Help connecting to linux server from shh client in windows!

I just installed the latest version of unbuntu server and want to connect from windows using a ssh client. This is my first linux server, so bare with me =) For my server... cat /etc/hostsgives me 127.0.0.1 localhost 127.0.1.1 ubuntuHomeI tried using ssh secure shell and putty in vista.... (11 Replies)
Discussion started by: Bandit390
11 Replies

7. Linux

running Client-Server prog on 2 LInux boxes

hi , i have 2 linux boxes with linksys wireless adapters everything is working fine.now i am trying to run client-server program written in C on these boxes but none of the box is receiving any packets. how different it is when using wireless . All these codes that i have works fine on Unix though... (1 Reply)
Discussion started by: phantom308
1 Replies

8. UNIX for Dummies Questions & Answers

socket programming : client server IPC

I'm new to socket programming. Have a basic doubt. I have a structure(global) at the server side. I have two different client connecting to the server. Will the changes made by one client on the structure be visible to the other client when it accesses the same client? I'm creating a STREAM... (3 Replies)
Discussion started by: abc.working
3 Replies

9. Programming

UDP socket - can both client and server recv and send

Hi, Am very new to socket programming. When we use UDP sockets to communicate between two processess, will both the client/server socket be able to send/recv ? meaning can sendto()/ recvfrom() be used on both server and client? It could be useful even if anybody provide some link on socket... (1 Reply)
Discussion started by: rvan
1 Replies

10. UNIX for Dummies Questions & Answers

simple client/server in linux

i am working on a C code, i have client and server client sends "list <foldername>" server needs to copy the files in the folder to a string. so that i can send that string back to the client. i heard there is a function to handle this in linux, do you know? (1 Reply)
Discussion started by: najdorf
1 Replies
Login or Register to Ask a Question