Category Archives: C Network Programming

Socket Programming (UDP) using C:

Socket Programming User Datagram Protocol (UDP):
-Please have look at [TCP Programming in C – youtube.com/vinodthebest]
-Server & Client Concept.
-Major steps.
-Server:
=> Create a socket with the socket()
=> Bind the socket to an address using the bind().
=> Send and receive data, use the recvfrom() and sendto() system calls.

-Client:
=> Create a socket with the socket()
=> Send and receive data, use the recvfrom() and sendto() system calls.

-Server coding.
-Client coding.
-Running.

Coding

Server:


/* Creates a datagram server. The port number is passed as an argument. This server runs forever */

#include
#include
#include
#include
#include

void error(char *msg)
{
perror(msg);
exit(0);
}

int main(int argc, char *argv[])
{
int sock, length, fromlen, n;
struct sockaddr_in server;
struct sockaddr_in from;
char buf[1024];

if (argc < 2)
{
fprintf(stderr, "ERROR, no port provided\n");
exit(0);
}

sock=socket(AF_INET, SOCK_DGRAM, 0);
if (sock < 0)
{
error("Opening socket");
}
length = sizeof(server);
bzero(&server,length);
server.sin_family=AF_INET;
server.sin_addr.s_addr=INADDR_ANY;
server.sin_port=htons(atoi(argv[1]));
if (bind(sock,(struct sockaddr *)&server,length)<0)
{
error("binding");
}
fromlen = sizeof(struct sockaddr_in);
while (1)
{
n = recvfrom(sock,buf,1024,0,(struct sockaddr *)&from,&fromlen);
if (n < 0)
{
error("recvfrom");
}
write(1,"Received a datagram: ",21);
write(1,buf,n);
n = sendto(sock,"Got your message\n",17,
0,(struct sockaddr *)&from,fromlen);
if (n < 0)
{
error("sendto");
}
}
}

Client:

/* UDP client in the internet domain */

#include
#include
#include
#include
#include
#include

void error(char *);

int main(int argc, char *argv[])
{
int sock, length, n;
struct sockaddr_in server, from;
struct hostent *hp;
char buffer[256];

if (argc != 3)
{
printf("Usage: server port\n");
exit(1);
}

sock= socket(AF_INET, SOCK_DGRAM, 0);

if (sock h_addr, (char *)&server.sin_addr,hp->h_length);
server.sin_port = htons(atoi(argv[2]));
length=sizeof(struct sockaddr_in);

printf("Please enter the message: ");

bzero(buffer,256);
fgets(buffer,255,stdin);
n=sendto(sock,buffer,strlen(buffer),0,&server,length);
if (n < 0)
{
error("Sendto");
}
n = recvfrom(sock,buffer,256,0,&from, &length);
if (n < 0)
{
error("recvfrom");
}
write(1,"Got an ack: ",12);
write(1,buffer,n);
}

void error(char *msg)
{
perror(msg);
exit(0);
}

Advertisements

6 Comments

Filed under C Network Programming, C Programming

Socket Programming (TCP) using C:

Socket Programming:
-Minimum Requirement.
-Linux OS [Lib. Header Files]
-IP & Port No.
-Server & Client Concept.
-Major steps.
-Server:
=Create a socket with the socket()
=Bind the socket to an address using the bind().
=Listen for connections with the listen().
=Accept a connection with the accept().
=Send and receive data, use the read() and write() system calls.

-Client:
=Create a socket with the socket()
=Connect the socket to the address of the server using the connect() system call.
=Send and receive data, use the read() and write() system calls.
-Server coding.
-Client coding.
-Running.

Coding:

Server :

/* A simple server in the internet domain using TCP the port number is passed as an argument */

#include
#include
#include
#include

void error(char *msg)
{
perror(msg);
exit(1);
}

int main(int argc, char *argv[])
{

int sockfd, newsockfd, portno, clilen;
char buffer[256];
struct sockaddr_in serv_addr, cli_addr;
int n;

if (argc < 2)
{
fprintf(stderr,"ERROR, no port provided\n");
exit(1);
}

sockfd = socket(AF_INET, SOCK_STREAM, 0);

if (sockfd < 0)
{
error("ERROR opening socket");
}

bzero((char *) &serv_addr, sizeof(serv_addr));

portno = atoi(argv[1]);

serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = INADDR_ANY;
serv_addr.sin_port = htons(portno);

if (bind(sockfd, (struct sockaddr *) &serv_addr,sizeof(serv_addr)) < 0)
{
error("ERROR on binding");
}

listen(sockfd,5);
clilen = sizeof(cli_addr);

newsockfd = accept(sockfd,(struct sockaddr *) &cli_addr, &clilen);

if (newsockfd < 0)
{
error("ERROR on accept");
}

bzero(buffer,256);

n = read(newsockfd,buffer,255);

if (n < 0)
{
error("ERROR reading from socket");
}

printf("Here is the message: %s\n",buffer);

n = write(newsockfd,"I got your message",18);

if (n < 0)
{
error("ERROR writing to socket");
}
return 0;
}

Client :


#include
#include
#include
#include
#include

void error(char *msg)
{
perror(msg);
exit(0);
}

int main(int argc, char *argv[])
{
int sockfd, portno, n;
struct sockaddr_in serv_addr;
struct hostent *server;

char buffer[256];

if (argc < 3)
{
fprintf(stderr,"usage %s hostname port\n", argv[0]);
exit(0);
}

portno = atoi(argv[2]);

sockfd = socket(AF_INET, SOCK_STREAM, 0);

if (sockfd h_addr,(char *)&serv_addr.sin_addr.s_addr,server->h_length);

serv_addr.sin_port = htons(portno);

if (connect(sockfd,&serv_addr,sizeof(serv_addr)) < 0)
{
error("ERROR connecting");
}
printf("Please enter the message: ");
bzero(buffer,256);
fgets(buffer,255,stdin);
n = write(sockfd,buffer,strlen(buffer));
if (n < 0)
{
error("ERROR writing to socket");
}
bzero(buffer,256);
n = read(sockfd,buffer,255);
if (n < 0)
{
error("ERROR reading from socket");
}
printf("%s\n",buffer);

return 0;
}

17 Comments

Filed under C Network Programming, C Programming