Sponsored Content
Top Forums Programming Socket dual client/server Linux Post 302924292 by godna on Friday 7th of November 2014 02:54:47 PM
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);
}

 

10 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

5. 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

6. 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

7. 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

8. 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

9. 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

10. 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
All times are GMT -4. The time now is 02:27 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy