This is my first post and it is about a very simple program using sockets on linux. The aim is not to have a clean program that handles errors, does safe communication etc, but a very simplistic introduction of sockets and network communications.
For understanding this article, you will need basic knowledge in C programming.
The application is made of 2 programs:
- A server program that opens a socket and the constantly listen on the specified port. When it receives a message, it displays the received message and send the same message back.
- A client program that opens a socket and connects to the server on the specified address and sends a text message to the server. It then receives the message back and displays it before exiting the program.
#include <stdio.h>And here is the code for the sockets_client.c
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <string.h>
#include <arpa/inet.h>
#define MSGSIZE 8192
main(int argc, char **argv)
{
char msg[MSGSIZE], msg_back[MSGSIZE];
int sd, sd_current, cc, fromlen, tolen;
int addrlen, port;
struct sockaddr_in sin;
struct sockaddr_in pin;
//Wrong number of arguments
if (argc!=2)
{
printf ("Incorrect number of argument\n");
printf ("Usage : %s port\n", argv[0]);
return 1;
}
//parsing the parameters
sscanf (argv[1],"%d",&port);
/* get an internet domain socket */
sd = socket(AF_INET, SOCK_STREAM, 0);
/* complete the socket structure */
memset(&sin, 0, sizeof(sin));
sin.sin_family = AF_INET;
sin.sin_addr.s_addr = INADDR_ANY;
sin.sin_port = htons(port);
/* bind the socket to the port number */
bind(sd, (struct sockaddr *) &sin, sizeof(sin));
/* show that we are willing to listen */
listen(sd, 5);
/* loop accepting connections */
while(1){
/* wait for a client to talk to us */
addrlen = sizeof(pin);
sd_current = accept(sd, (struct sockaddr *) &pin, &addrlen);
printf("Connection established with %s\n", inet_ntoa(pin.sin_addr));
/* recieve a message from the client */
recv(sd_current, msg, sizeof(msg), 0);
printf("Message recieved: %s from %s\n",msg, inet_ntoa(pin.sin_addr));
/* send the answer message to the client */
send(sd_current, msg, strlen(msg), 0);
printf("Message sent: %s to %s\n",msg, inet_ntoa(pin.sin_addr));
/* close up the socket with the client */
close(sd_current);
}
}
#include <stdio.h>To compile that code, you will need to run the following commands:
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <string.h>
#define HOSTSIZE 256
#define MSGSIZE 8192
main(int argc, char **argv)
{
char host[HOSTSIZE];
int sd, port;
struct sockaddr_in sin;
struct sockaddr_in pin;
struct hostent *hp;
char msg[MSGSIZE];
//Wrong number of arguments
if (argc!=4)
{
printf ("Incorrect number of argument\n");
printf ("Usage : %s host port msg\n", argv[0]);
return 1;
}
//parsing the parameters
sscanf (argv[1],"%s",host);
sscanf (argv[2],"%d",&port);
strcpy (msg,argv[3]);
/* go find out about the desired host machine */
hp = gethostbyname(host);
/* fill in the socket structure with host information */
memset(&pin, 0, sizeof(pin));
pin.sin_family = AF_INET;
pin.sin_addr.s_addr = ((struct in_addr *)(hp->h_addr))->s_addr;
pin.sin_port = htons(port);
/* grab an Internet domain socket */
sd = socket(AF_INET, SOCK_STREAM, 0);
/* connect to PORT on HOST */
connect(sd,(struct sockaddr *) &pin, sizeof(pin));
/* send a message to the server PORT on machine HOST */
send(sd, msg, strlen(msg), 0);
printf ("Sent message : %s \n", msg);
/* receive a message back */
recv(sd, msg, sizeof(msg), 0);
printf ("Received message back: %s \n", msg);
close(sd);
}
gcc sockets_client.c -o clientThen to start the server listening on port 1507, you need to run:
gcc sockets_server.c -o server
./server 1507And finally, to start the client and to send the message "Hello World" on the same machine on port 1507:
./client 127.0.0.1 1507 "Hello World"
Have fun!
No comments:
Post a Comment