0% found this document useful (0 votes)
65 views7 pages

Socket Programming (Single Client)

This document discusses socket programming in C using TCP and UDP. It provides code examples for a TCP client and server that allow a client to send a message to the server and receive an acknowledgment back. It also includes code examples for a UDP client and server that enable a client to send a datagram to a server and receive a response datagram back.

Uploaded by

Shyam Chonat
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
65 views7 pages

Socket Programming (Single Client)

This document discusses socket programming in C using TCP and UDP. It provides code examples for a TCP client and server that allow a client to send a message to the server and receive an acknowledgment back. It also includes code examples for a UDP client and server that enable a client to send a datagram to a server and receive a response datagram back.

Uploaded by

Shyam Chonat
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 7

Socket Programming

Single Client

TCP
TCP client
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include<arpa/inet.h>

int main()
{
int sockfd,n;
struct sockaddr_in serv_addr;
char buffer[256];

sockfd = socket(AF_INET, SOCK_STREAM, 0);

serv_addr.sin_family = AF_INET;
serv_addr.sin_port = htons(5013);
serv_addr.sin_addr.s_addr=inet_addr("127.0.0.1");

n=connect(sockfd,(struct sockaddr *) &serv_addr,sizeof(serv_addr));

memset(buffer,0,256);

printf("Please enter the message: ");


fgets(buffer,255,stdin);

n = write(sockfd,buffer,strlen(buffer));

memset(buffer,0,256);

n = read(sockfd,buffer,255);

write(1,"ACK: ",5);
write(1,buffer,n);

close(sockfd);
return 0;
}

TCP server

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>

int main()
{
int sockfd, newsockfd;
char buffer[256];
int n;

struct sockaddr_in serv_addr;

sockfd = socket(AF_INET, SOCK_STREAM, 0);

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

bind(sockfd, (struct sockaddr *) &serv_addr,sizeof(serv_addr));

listen(sockfd,5);

while(1)
{
newsockfd = accept(sockfd,(struct sockaddr *)NULL,NULL);
memset(buffer,0,256);

n = read(sock,buffer,255);

write(1,"Received a datagram: ",21);


write(1,buffer,n);

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

close(newsockfd);
}
close(sockfd);
return 0;
}

UDP
UDP server
#include<sys/types.h>
#include<netinet/in.h>
#include<sys/socket.h>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>

int main()
{
int socketid,length,clientlength,senrec;
struct sockaddr_in server;
struct sockaddr_in client;

char buffer[256];

socketid=socket(AF_INET,SOCK_DGRAM,0);
length=sizeof(server);
memset(&server,0,length);
server.sin_family=AF_INET;
server.sin_addr.s_addr=htonl(INADDR_ANY);
server.sin_port=htons(5050);

senrec=bind(socketid,(struct sockaddr *)&server,length);


clientlength=sizeof(struct sockaddr_in);

while(1)
{
senrec=recvfrom(socketid,buffer,256,0,(struct sockaddr
*)&client,&clientlength);
write(1,"Received a datagram: ",21);
write(1,buffer,senrec);
senrec=sendto(socketid,"Got your message\n",17,0,(struct sockaddr
*)&client,clientlength);
}
close(socketid);
return 0;
}
UDP client
#include<sys/types.h>
#include<netinet/in.h>
#include<sys/socket.h>

#include<netdb.h>
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<arpa/inet.h>
int main()
{
int socketid,length,senrec;
struct sockaddr_in server,client;
char buf[256];

socketid=socket(AF_INET,SOCK_DGRAM,0);
server.sin_family=AF_INET;
server.sin_port=htons(5050);
server.sin_addr.s_addr=inet_addr("127.0.0.1");
length=sizeof(struct sockaddr_in);
memset(buf,0,256);

printf("enter message");
fgets(buf,255,stdin);

senrec=sendto(socketid,buf,strlen(buf),0,(struct sockaddr *)&server,length);


senrec=recvfrom(socketid,buf,256,0,(struct sockaddr *)&client,&length);

write(1,"ACK: ",5);
write(1,buf,senrec);

close(socketid);
return 0;
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy