multicast data from specific ip address - c

For example I am having two different segment IP of same server 172.17.8.90 172.19.8.100. Now, I want my program to send multicast from only 172.19.8.100 this ip. Currently my program is sending multicast from 172.17.8.90 which is not allowed to send multicast because of this, i want multicast data from specific IP
#include <stdio.h> /* for fprintf() */
#include <sys/socket.h> /* for socket(), connect(), send(), and recv() */
#include <arpa/inet.h> /* for sockaddr_in and inet_addr() */
#include <stdlib.h> /* for atoi() and exit() */
#include <string.h> /* for memset() */
#include <unistd.h> /* for sleep() */
void DieWithError(char *errorMessage) /* External error handling function */
{
fprintf(stderr,"Dieing baby\n");
}
int main(int argc, char *argv[])
{
int sock; /* Socket */
struct sockaddr_in multicastAddr; /* Multicast address */
char *multicastIP; /* IP Multicast address */
unsigned short multicastPort; /* Server port */
char *sendString; /* String to multicast */
unsigned char multicastTTL; /* TTL of multicast packets */
unsigned int sendStringLen; /* Length of string to multicast */
if ((argc < 4) || (argc > 5)) /* Test for correct number of parameters */
{
fprintf(stderr,"Usage: %s <Multicast Address> <Port> <Send String> [<TTL>]\n",
argv[0]);
exit(1);
}
multicastIP = argv[1]; /* First arg: multicast IP address */
multicastPort = atoi(argv[2]); /* Second arg: multicast port */
sendString = argv[3]; /* Third arg: String to multicast */
if (argc == 5) /* Is TTL specified on command-line? */
multicastTTL = atoi(argv[4]); /* Command-line specified TTL */
else
multicastTTL = 1; /* Default TTL = 1 */
/* Create socket for sending/receiving datagrams */
if ((sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0)
DieWithError("socket() failed");
/* Set TTL of multicast packet */
if (setsockopt(sock, IPPROTO_IP, IP_MULTICAST_TTL, (void *) &multicastTTL,
sizeof(multicastTTL)) < 0)
DieWithError("setsockopt() failed");
/* Construct local address structure */
memset(&multicastAddr, 0, sizeof(multicastAddr)); /* Zero out structure */
multicastAddr.sin_family = AF_INET; /* Internet address family */
multicastAddr.sin_addr.s_addr = inet_addr(multicastIP);/* Multicast IP address */
multicastAddr.sin_port = htons(multicastPort); /* Multicast port */
sendStringLen = strlen(sendString); /* Find length of sendString */
for (;;) /* Run forever */
{
/* Multicast sendString in datagram to clients every 3 seconds */
if (sendto(sock, sendString, sendStringLen, 0, (struct sockaddr *)
&multicastAddr, sizeof(multicastAddr)) != sendStringLen)
DieWithError("sendto() sent a different number of bytes than expected");
sleep(3);
}
}

When sending out multicast packets, the OS will by default chose one particular interface to send from. To change the outgoing interface, you need to set the IP_MULTICAST_IF option:
struct in_addr multi_out;
multi_out.s_addr = inet_addr("172.19.8.100");
if (setsockopt(sock,IPPROTO_IP,IP_MULTICAST_IF, (char *)&multi_out, sizeof(multi_out)) == -1) {
perror("error setting IP_MULTICAST_IF option");
exit(1);
}
Also be sure to use the perror function as above for error checking to see why a particular library call failed.

Related

server client program arguments?

hello i was recently given this server/client program in c that is supposed to be okay and not have any mistakes. problem is every time i try to run it i get the message the program is supposed to print when there are not enough arguments (even though i give the port number and the message i want it to echo) could anyone help me find what i am doing wrong? thank you in advance for any help you give me. the code i wanna run is the following:
server :
#include <stdio.h> /* for printf() and fprintf() */
#include <sys/socket.h> /* for socket(), connect(), send(), and recv() */
#include <arpa/inet.h> /* for sockaddr_in and inet_addr() */
#include <stdlib.h> /* for atoi() */
#include <string.h> /* for memset() */
#include <unistd.h> /* for close() */
#define MAXPENDING 5 /* Maximum outstanding connection requests */
#define RCVBUFSIZE 32 /*Size of receive buffer*/
void DieWithError(char *errorMessage); /* Error handling function */
void HandleTCPClient(int clntSocket);/* TCP client handling function */
int main(int argc, char *argv[]) {
int servSock; /* Socket descriptor for server */
int clntSock; /* Socket descriptor for client */
struct sockaddr_in echoServAddr; /* Local address */
struct sockaddr_in echoClntAddr; /* Client address */
unsigned short echoServPort; /* Server port */
unsigned int clntLen; /* Length of client address data structure */
if (argc != 2) {/* Test for correct number of arguments */
fprintf(stderr, "Usage: %s <Server Port>\n", argv[0]) ;
exit(1);
}
echoServPort = atoi(argv[1]); /* First arg: local port */
/* Create socket for incoming connections */
if ((servSock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0)
DieWithError( "socket () failed") ;
/* Construct local address structure */
memset(&echoServAddr, 0, sizeof(echoServAddr)); /* Zero out structure */
echoServAddr.sin_family = AF_INET; /* Internet address family */
echoServAddr.sin_addr.s_addr = htonl(INADDR_ANY); /* Any incoming interface */
echoServAddr.sin_port = htons(echoServPort); /* Local port */
/* Bind to the local address */
if (bind(servSock, (struct sockaddr *)&echoServAddr, sizeof(echoServAddr)) < 0)
DieWithError ( "bind () failed");
/* Mark the socket so it will listen for incoming connections */
if (listen(servSock, MAXPENDING) < 0)
DieWithError("listen() failed") ;
for (;;) {/* Run forever */
/* Set the size of the in-out parameter */
clntLen = sizeof(echoClntAddr);
/* Wait for a client to connect */
if ((clntSock = accept(servSock, (struct sockaddr *) &echoClntAddr, &clntLen)) < 0)
DieWithError("accept() failed");
/* clntSock is connected to a client! */
printf("Handling client %s\n", inet_ntoa(echoClntAddr.sin_addr));
HandleTCPClient (clntSock) ;
}
/* NOT REACHED */
}
void DieWithError(char *errorMessage){
perror(errorMessage);
exit(1);
}
void HandleTCPClient(int clntSocket) {
char echoBuffer[RCVBUFSIZE];/* Buffer for echo string */
int recvMsgSize;/* Size of received message */
/* Receive message from client */
if ((recvMsgSize = recv(clntSocket, echoBuffer, RCVBUFSIZE, 0)) < 0)
DieWithError("recv() failed") ;
/* Send received string and receive again until end of transmission */
while (recvMsgSize > 0){ /* zero indicates end of transmission */
/* Echo message back to client */
if (send(clntSocket, echoBuffer, recvMsgSize, 0) != recvMsgSize)
DieWithError("send() failed");
/* See if there is more data to receive */
if ((recvMsgSize = recv(clntSocket, echoBuffer, RCVBUFSIZE, 0)) < 0)
DieWithError("recv() failed") ;
}
close(clntSocket); /* Close client socket */
}
the client is the following:
#include <stdio.h> /* for printf() and fprintf() */
#include <sys/socket.h> /* for socket(), connect(), send(), and recv() */
#include <arpa/inet.h> /* for sockaddr_in and inet_addr() */
#include <stdlib.h> /* for atoi() */
#include <string.h> /* for memset() */
#include <unistd.h> /* for close() */
#define RCVBUFSIZE 32 /* Size of receive buffer */
void DieWithError(char *errorMessage); /* Error handling function */
int main(int argc, char *argv[]) {
int sock; /* Socket descriptor */
struct sockaddr_in echoServAddr; /* Echo server address */
unsigned short echoServPort; /* Echo server port */
char *servIP; /* Server IP address (dotted quad) */
char *echoString; /* String to send to echo server */
char echoBuffer[RCVBUFSIZE]; /* Buffer for echo string */
unsigned int echoStringLen; /* Bytes read in single recv()*/
int bytesRcvd, totalBytesRcvd; /*total bytes read */
if ((argc<3) || (argc>4)) {
fprintf(stderr, "Usage: %s <Server IP> <Echo Word> [<Echo Port>]\n", argv[0]);
exit(1);
}
servIP = argv[1] ; /* First arg' server IP address (dotted quad) */
echoString = argv[2] ;/* Second arg' string to echo */
if (argc == 4)
echoServPort = atoi(argv[3]); /* Use given port, if any */
else
echoServPort = 7; /* 7 is the well-known port for the echo service */
/* Create a reliable, stream socket using TCP */
if ((sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0)
DieWithError(" socket () failed") ;
/* Construct the server address structure */
memset(&echoServAddr, 0, sizeof(echoServAddr)); /* Zero out structure */
echoServAddr.sin_family = AF_INET; /* Internet address family */
echoServAddr.sin_addr.s_addr = inet_addr(servIP); /* Server IP address */
echoServAddr.sin_port = htons(echoServPort); /* Server port */
/* Establish the connection to the echo server */
if (connect(sock, (struct sockaddr *) &echoServAddr, sizeof(echoServAddr)) < 0)
DieWithError(" connect () failed") ;
echoStringLen = strlen(echoString) ; /* Determine input length */
/* Send the string to the server */
if (send(sock, echoString, echoStringLen, 0) != echoStringLen)
DieWithError("send() sent a different number of bytes than expected");
/* Receive the same string back from the server */
totalBytesRcvd = 0;
printf("Received: "); /* Setup to print the echoed string */
while (totalBytesRcvd < echoStringLen) {
/* Receive up to the buffer size (minus 1 to leave space for
a null terminator) bytes from the sender */
if ((bytesRcvd = recv(sock, echoBuffer, RCVBUFSIZE - 1, 0)) <= 0)
DieWithError("recv() failed or connection closed prematurely");
totalBytesRcvd += bytesRcvd; /* Keep tally of total bytes */
echoBuffer[bytesRcvd] = '\0'; /* Terminate the string! */
printf(echoBuffer); /* Print the echo buffer */
}
printf("\n"); /* Print a final linefeed */
close(sock);
exit(0);
}
void DieWithError(char *errorMessage){
perror(errorMessage);
exit(1);
}
Correct me if I'm wrong, but I assume the problem is that you are inputting the wrong parameters, and are getting the "Usage: ..." error message?
Provided you compile it correctly, the programs should run with something like the following commands:
./server 1200
./client localhost message 1200
The server program accepts only 1 parameter, and the client program accepts 2 or 3 parameters. The server should also be run before the client.

Checking return address in recvfrom() (C UDP Sockets)

This pertains to attempting to validate the return address in recvfrom() (the fifth argument to the function) in this UDP echo client:
While I can send data to the server and receive return communications correctly, I'm having trouble validating the return IP address when comparing the fromAddr.sin_addr and echoServAddr.sin_addr.
The goal here is to compare the address in the structure that was used in sendto() and the address in the structure returned from recvfrom() to validate the echo reply from the server indeed came from where the initial transmission from the client was sent (A rudimentary POC that there wasn't a Man In The Middle).
What should I be looking at in order to appropriately validate the return address as it is returned from recvfrom() matches the address transmitted to as referenced in the sendto() call?
#include <stdio.h> /* for printf() and fprintf() */
#include <sys/socket.h> /* for socket(), connect(), sendto(), and recvfrom() */
#include <arpa/inet.h> /* for sockaddr_in and inet_addr() */
#include <stdlib.h> /* for atoi() and exit() */
#include <string.h> /* for memset() */
#include <unistd.h> /* for close() */
#define ECHOMAX 255 /* Longest string to echo */
void DieWithError(char *errorMessage); /* External error handling function */
int main(int argc, char *argv[])
{
int sock; /* Socket descriptor */
struct sockaddr_in echoServAddr; /* Echo server address */
struct sockaddr_in fromAddr; /* Source address of echo */
unsigned short echoServPort; /* Echo server port */
unsigned int fromSize; /* In-out of address size for recvfrom() */
char *servIP; /* IP address of server */
char *echoString; /* String to send to echo server */
char echoBuffer[ECHOMAX+1]; /* Buffer for receiving echoed string */
int echoStringLen; /* Length of string to echo */
int respStringLen; /* Length of received response */
// manage the command line arguments and errors
if ((argc < 3) || (argc > 4)) {
fprintf(stderr, "Usage: %s <Server IP> <Echo Word> [<Echo Port>]\n",
argv[0]);
exit(1);
}
// load servIP
servIP = argv[1];
// load echoString
echoString = argv[2];
// check echoString and error if too long
echoStringLen = strlen(echoString);
if (!(echoStringLen <= 255)) {
DieWithError("BUFFER EXCEEDED ERROR");
}
// load port
if (argc == 4)
echoServPort = atoi(argv[3]);
else
echoServPort = 7;
// create the socket
if ((sock = socket(PF_INET, SOCK_DGRAM, 0)) < 0) {
DieWithError("SOCKET CREATION ERROR");
}
/* Construct the server address structure */
memset(&echoServAddr, 0, sizeof(echoServAddr)); /* Zero out structure */
echoServAddr.sin_family = AF_INET; /* Internet addr family */
echoServAddr.sin_addr.s_addr = inet_addr(servIP); /* Server IP address */
echoServAddr.sin_port = htons(echoServPort); /* Server port */
// send the string
inet_pton(AF_INET, servIP, &echoServAddr.sin_addr);
if ((sendto(sock, echoString, strlen(echoString), 0, (struct sockaddr *)
&echoServAddr, sizeof(echoServAddr))) < 0) {
DieWithError("SEND ERROR");
}
// recieve a response
respStringLen = recvfrom(sock, echoBuffer, sizeof(echoBuffer), 0,
(struct sockaddr *) &fromAddr, &fromSize);
if (respStringLen < 0) {
DieWithError("RECV ERROR");
}
// print the message sent
printf("SENT FROM CLIENT: '%s'\n", echoString);
// print the message recieved
echoBuffer[respStringLen] = '\0';
printf("RECEIVED FROM SERVER: '%s'\n", echoBuffer);
// check if from the correct server
if ((struct sockaddr *) &echoServAddr.sin_addr != (struct sockaddr *) &fromAddr.sin_addr) {
DieWithError("INVALID RETURN ADDRESS");
}
// close the socket
close(sock);
// exit the program
exit(0);
}
If the source IP/port of the incoming packet is the same as the destation IP/port of the packet you sent, then the sin_addr and sin_port fields of echoServAddr and fromAddr should match.
This line of code however doesn't do that:
if ((struct sockaddr *) &echoServAddr.sin_addr != (struct sockaddr *) &fromAddr.sin_addr) {
This is comparing the address of echoServAddr.sin_addr against the address of fromAddr.sin_addr. Because these are two separate variables, this will always be false. You instead want:
if ((echoServAddr.sin_addr.s_addr != fromAddr.sin_addr.s_addr) ||
(echoServAddr.sin_port != fromAddr.sin_port))

Do we have to write our own arpa/inet.h?

#include <stdio.h> /* standard C i/o facilities */
#include <stdlib.h> /* needed for atoi() */
#include <unistd.h> /* defines STDIN_FILENO, system calls,etc */
#include <sys/types.h> /* system data type definitions */
#include <sys/socket.h> /* socket specific definitions */
#include <netinet/in.h> /* INET constants and stuff */
#include <arpa/inet.h> /* IP address conversion stuff */
#include <netdb.h> /* gethostbyname */
/* this routine echos any messages (UDP datagrams) received */
#define MAXBUF 1024*1024
void echo( int sd ) {
int len,n;
char bufin[MAXBUF];
struct sockaddr_in remote;
/* need to know how big address struct is, len must be set before the
call to recvfrom!!! */
len = sizeof(remote);
while (1) {
/* read a datagram from the socket (put result in bufin) */
n=recvfrom(sd,bufin,MAXBUF,0,(struct sockaddr *)&remote,&len);
/* print out the address of the sender */
printf("Got a datagram from %s port %d\n",
inet_ntoa(remote.sin_addr), ntohs(remote.sin_port));
if (n<0) {
perror("Error receiving data");
} else {
printf("GOT %d BYTES\n",n);
/* Got something, just send it back */
sendto(sd,bufin,n,0,(struct sockaddr *)&remote,len);
}
}
}
/* server main routine */
int main() {
int ld;
struct sockaddr_in skaddr;
int length;
/* create a socket
IP protocol family (PF_INET)
UDP protocol (SOCK_DGRAM)
*/
if ((ld = socket( PF_INET, SOCK_DGRAM, 0 )) < 0) {
printf("Problem creating socket\n");
exit(1);
}
/* establish our address
address family is AF_INET
our IP address is INADDR_ANY (any of our IP addresses)
the port number is assigned by the kernel
*/
skaddr.sin_family = AF_INET;
skaddr.sin_addr.s_addr = htonl(INADDR_ANY);
skaddr.sin_port = htons(0);
if (bind(ld, (struct sockaddr *) &skaddr, sizeof(skaddr))<0) {
printf("Problem binding\n");
exit(0);
}
/* find out what port we were assigned and print it out */
length = sizeof( skaddr );
if (getsockname(ld, (struct sockaddr *) &skaddr, &length)<0) {
printf("Error getsockname\n");
exit(1);
}
/* port number's are network byte order, we have to convert to
host byte order before printing !
*/
printf("The server UDP port number is %d\n",ntohs(skaddr.sin_port));
/* Go echo every datagram we get */
echo(ld);
return(0);
}
I am new to linux and have booted linux on my zedboard following some tutorials. Now I have to make an application program that runs udp echo server over it and I got this program over internet and now I am confused whether the arpa/inet.h netinet/in.h are already available in linux or do we have to make those? and How and where should I give the ip address and port numbers?

Broadcasting a message to multiple clients from a server using UDP protocol

i have two code here for a UDP chat with a broadcasting mechanism where the server can broadcast a message to all clients simultaneously
Server code...
#include<stdio.h>
#include<string.h>
#include<sys/stat.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<arpa/inet.h>
int main(int argc, char *argv[] )
{
struct sockaddr_in client,server;
int s,n;
char b1[100],b2[100];
s=socket(AF_INET,SOCK_DGRAM,0);
server.sin_family=AF_INET;
server.sin_port=htons(2000);
server.sin_addr.s_addr=inet_addr("127.0.0.1");
bind(s,(struct sockaddr *)&server,sizeof(server));
printf("\nServer ready,waiting for client....\n");
n=sizeof(client);
int sock; /* Socket */
struct sockaddr_in broadcastAddr; /* Broadcast address */
char *broadcastIP; /* IP broadcast address */
unsigned short broadcastPort; /* Server port */
char *sendString; /* String to broadcast */
int broadcastPermission; /* Socket opt to set permission to broadcast */
unsigned int sendStringLen; /* Length of string to broadcast */
if (argc < 4) /* Test for correct number of parameters */
{
fprintf(stderr,"Usage: %s <IP Address> <Port> <Send String>\n", argv[0]);
exit(1);
}
broadcastIP = argv[1]; /* First arg: broadcast IP address */
broadcastPort = atoi(argv[2]); /* Second arg: broadcast port */
sendString = argv[3]; /* Third arg: string to broadcast */
/* Create socket for sending/receiving datagrams */
sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP);
broadcastPermission = 1;
while(1)
{
recvfrom(s,b1,sizeof(b1),0,(struct sockaddr *) &client,&n);
if(!(strcmp(b1,"end")))
break;
printf("\nClient:%s",b1);
printf("\nServer:");
gets(b2);
sendto(s,b2,sizeof(b2),0,(struct sockaddr *) &client,n);
broadcastPermission = 1;
setsockopt(sock, SOL_SOCKET, SO_BROADCAST, (void *) &broadcastPermission,sizeof(broadcastPermission));
/* Construct local address structure */
//memset(&broadcastAddr, 0, sizeof(broadcastAddr)); /* Zero out structure */
broadcastAddr.sin_family = AF_INET; /* Internet address family */
broadcastAddr.sin_addr.s_addr = inet_addr(broadcastIP);/* Broadcast IP address */
broadcastAddr.sin_port = htons(broadcastPort); /* Broadcast port */
sendStringLen = strlen(sendString);
for (;;) /* Run forever */
{
/* Broadcast sendString in datagram to clients every 3 seconds*/
sendto(sock, sendString, sendStringLen, 0, (struct sockaddr *) &broadcastAddr, sizeof(broadcastAddr));
// DieWithError("sendto() sent a different number of bytes than expected");
//sleep(3); /* Avoids flooding the network */
}
}
}
Client code...
#include<stdio.h>
#include<string.h>
#include<sys/stat.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<arpa/inet.h>
#define MAXRECVSTRING 255
int main(int argc , char argv[])
{
struct sockaddr_in client,server;
int s,n;
char b1[100],b2[100];
s=socket(AF_INET,SOCK_DGRAM,0);
server.sin_family=AF_INET;
server.sin_port=htons(2000);
server.sin_addr.s_addr=inet_addr("127.0.0.1");
printf("\nClient ready....\n");
n=sizeof(server);
int sock; /* Socket */
struct sockaddr_in broadcastAddr; /* Broadcast Address */
unsigned short broadcastPort; /* Port */
char recvString[MAXRECVSTRING+1]; /* Buffer for received string */
int recvStringLen; /* Length of received string */
if (argc != 2) /* Test for correct number of arguments */
{
fprintf(stderr,"Usage: %s <Broadcast Port>\n", argv[0]);
exit(1);
}
printf("\nClient ready…11111.\n");
broadcastPort = htons(argv[1]); /* First arg: broadcast port */
printf("\nClient ready….1\n");
/* Create a best-effort datagram socket using UDP */
if(sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP)<0)
printf("no sock created");
printf("\nClient ready….2\n");
while(1)
{
printf("\nClient:");
gets(b2);
sendto(s,b2,sizeof(b2),0,(struct sockaddr *) &server,n);
if(strcmp(b2,"end")==0)
break;
recvfrom(s,b1,sizeof(b1),0,NULL,NULL);
printf("\nServer:%s",b1);
broadcastAddr.sin_family = AF_INET; /* Internet address family */
broadcastAddr.sin_addr.s_addr = htonl(INADDR_ANY); /* Any incoming interface */
broadcastAddr.sin_port = htons(broadcastPort); /* Broadcast port */
/* Bind to the broadcast port */
bind(sock, (struct sockaddr *) &broadcastAddr, sizeof(broadcastAddr));
/* Receive a single datagram from the server */
recvStringLen = recvfrom(sock, recvString, MAXRECVSTRING, 0, NULL, 0);
recvString[recvStringLen] = '\0';
printf("Received: %s\n", recvString); /* Print the received string */
}
close(sock);
}
These code run without any error but when i send a string from client to the server the server is unable to receive the string and vice versa.
If someone could help me out as to why this is happening.
i am running both the server and client on the same system in different terminal windows..hence the local host address
Some platforms, e.g. Windows, won't receive broadcasts unless the socket is bound to INADDR_ANY, i.e. 0.0.0.0.
To start with:
server.sin_port=2000;
You forget to make this network-order.
In your server code:
The socket is not bound to any IP address. Although it broadcasts the message and the client receives; the vice-versa won't work.
Thus, bind your server and client side sockets to 2 different IP addresses.
When the server broadcasts the message, use appropriate broadcast address (see the subnet of the server-client IP) in the sendto function.

Daemon Process Acting as TCP Proxy Server

I have a C program that I am trying to implement a daemon process that acts as a general TCP proxy server. I believe I have completed mostly everything correct but there is a while loop that I cannot figure out. Below is my code and I will put *** around what I am having trouble with.
Program descriptions
(1) The daemon listens for TCP connections on a specified port number.
(2) When a new client initiates a TCP connection request, the daemon accepts the request and
establishes a new TCP connection with the new client.
(a) The daemon forks a child process that is dedicated to handling the new client.
(b) The child process establishes a new TCP connection to a pre-assigned port on the actual
targeted server.
(c) The child process falls into a loop in which it acts as an intermediator exchanging data
(reading/writing or writing/reading) between the client and the targeted server.
(3) Once a child has been forked, the daemon process resumes listening for additional TCP
connections from other clients.
#ifndef unix
#define closesocket close
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <stdlib.h>
#include <stdio.h>
#endif
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#define PROTOPORT 5100 /* default protocol port number */
#define QLEN 6 /* size of request queue */
int visits = 0;
int argc;
char *argv[];
int main() // works as "server" side
{
struct hostent *ptrh; /* pointer to a host table entry */
struct protoent *ptrp; /* pointer to a protocol table entry */
struct sockaddr_in sad; /* structure to hold server's address */
struct sockaddr_in cad; /* structure to hold client's address */
int sd0, sd1; /* socket descriptors */
int port; /* protocol port number */
int addrlen; /* length of address */
pid_t pid;
memset((char *)&sad, 0, sizeof(sad)); /* clear sockaddr structure */
sad.sin_family = AF_INET; /* set family to Internet */
sad.sin_addr.s_addr = INADDR_ANY; /* set the local IP address(not specify, reserved */
if(argc > 1){
port = atoi(argv[1]);
}
else port = PROTOPORT; /* use the specified port number */
if (port > 0) /* test for illegal value */
sad.sin_port = htons((u_short)port);
else
{ /* print error message and exit */
fprintf(stderr,"bad port number %s\n", argv[1]);
exit(1);
}
/* Map TCP transport protocol name to protocol number */
if( ((int)(ptrp = getprotobyname("tcp"))) == 0) {
fprintf(stderr, "cannot map \"tcp\" to protocol number");
exit(1);
}
/* Create a socket */
sd0 = socket(PF_INET, SOCK_STREAM, ptrp->p_proto);
if(sd0 < 0){
fprintf(stderr, "socket creation failed\n");
exit(1);
}
/* Bind a local address to the socket */
// bind()
if (bind(sd0, (struct sockaddr *)&sad, sizeof(sad)) < 0) {
fprintf(stderr,"bind failed\n");
exit(1);
}
/* Listen at the port */
// listen()
/* Specify size of request queue */
if (listen(sd0, QLEN) < 0) {
fprintf(stderr,"listen failed\n");
exit(1);
}
/* Main server loop - accept and handle requests */
while (1)
{
// accept request
addrlen = sizeof(cad);
sd1 = accept(sd0, (struct sockaddr *)&cad, &addrlen);
// fork a process
pid = fork();
if(pid == 0) // child process
{
//close oldsocket;
close(sd0);
HandleClientConnection(sd1);
break;
}
else /*parant process */
{
close(sd1);
continue;
}
}
return 0;
}
// implement "HandleClientConnection(new socket1)" - works as "client" side
HandleClientConnection(newsocket1)
{
// general socket settings at the client side
struct hostent *ptrh; /* pointer to a host table entry */
struct protoent *ptrp; /* pointer to a protocol table entry */
struct sockaddr_in sad; /* structure to hold an IP address */
int newsocket2; /* socket descriptor */
int port; /* protocol port number */
char *host; /* pointer to host name */
int n, n2; /* number of characters read */
char buf[1000]; /* buffer for data from the server */
int i;
memset((char *)&sad,0,sizeof(sad)); /* clear sockaddr structure */
sad.sin_family = AF_INET; /* set family to Internet */
***while(1) /* get the real server's IP addr from "GET" command */
{
// read to buffer and compare, such as
if(strncmp("GET", buf[i-1], 3) == 0 || strncmp("CONNECT", buf[i-1], 7) == 0)
{
while(1)
{
//...; // read more
if(strncmp("Host", buf[i-1], 4) == 0)
{
//...;
printf("\nfound the host name.\n");
//...;
}
}
break;
}
}***
/* Convert real host name to equivalent IP address and copy to sad. */
// gethostbyname();
ptrh = gethostbyname(host);
if ( ((char *)ptrh) == NULL )
{
fprintf(stderr,"invalid host: %s\n", host);
exit(1);
}
memcpy(&sad.sin_addr, ptrh->h_addr, ptrh->h_length);
/* Map TCP transport protocol name to protocol number. */
if ( ((int)(ptrp = getprotobyname("tcp"))) == 0) {
fprintf(stderr, "cannot map \"tcp\" to protocol number");
exit(1);
}
/* Create a newsocket2. */
// socket();
newsocket2 = socket(PF_INET, SOCK_STREAM, ptrp->p_proto);
if (newsocket2 < 0) {
fprintf(stderr, "socket creation failed\n");
exit(1);
}
/* Connect the socket to the specified server. */
// connect()
if (connect(newsocket2, (struct sockaddr *)&sad, sizeof(sad)) < 0) {
fprintf(stderr,"connect failed\n");
exit(1);
}
// Repeatedly read data from client (newsocket1) and write to server' side (newsocket2)
n = recv(newsocket1, buf, sizeof(buf), 0);
while (n > 0) {
write(newsocket2,buf,n);
n = recv(newsocket1, buf, sizeof(buf), 0);
}
// Repeatedly read data from server' side (newsocket2) and write to client (newsocket1).
n2 = recv(newsocket2, buf, sizeof(buf), 0);
while (n2 > 0) {
write(newsocket1,buf,n2);
n2 = recv(newsocket2, buf, sizeof(buf), 0);
}
// Close the newsocket2
close(newsocket2);
}

Resources