0% found this document useful (0 votes)
4 views8 pages

Program 2

The document contains a basic TCP client-server program in C, where the client sends a string to the server. The server listens for incoming connections, accepts a client request, and reads the data sent by the client. Both client and server use standard socket programming functions to establish communication and handle data transmission.

Uploaded by

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

Program 2

The document contains a basic TCP client-server program in C, where the client sends a string to the server. The server listens for incoming connections, accepts a client request, and reads the data sent by the client. Both client and server use standard socket programming functions to establish communication and handle data transmission.

Uploaded by

rajprithiv944
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

basic client and server program using tcp

program (client sending string and server


receiving data from client)

header.h
-------------------
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<unistd.h>
#include<netinet/in.h>
#include<netinet/ip.h>
#include<arpa/inet.h>
// server.c
#include"header.h"
void client_info(struct sockaddr_in client_info)
{
printf("client port address: %d\
n",client_info.sin_port);
printf("client ip address: %s\
n",inet_ntoa(client_info.sin_addr));
}
int main()
{
int sfd,len,nsfd;
struct sockaddr_in server_id,client_id;
/********** socket () ***************/
sfd=socket(AF_INET,SOCK_STREAM,0);
if(sfd<0)
{
perror("Socket");
return 0;
}
perror("Socket");
/*********** bind() ***************/
server_id.sin_family=AF_INET;
server_id.sin_port=htons(3000);
server_id.sin_addr.s_addr=inet_addr("[Link]");
len=sizeof(server_id);
if(bind(sfd,(struct sockaddr*) &server_id,len)<0)
{
perror("Bind");
return 0;
}
perror("Bind");
/************* listen **************/
if(listen(sfd,5)<0)
{
perror("Listen");
return 0;
}
perror("Listen");
/************ accept ****************/
printf("before accept client request...(waiting for
client)\n");
nsfd=accept(sfd,(struct
sockaddr*)&client_id,&len);
if(nsfd<0)
{
perror("Accept");
return 0;
}
printf("client request Accepted...\n");
client_info(client_id);
char s[20];
read(nsfd,s,sizeof(s)); // reading data from client
printf("client data: %s\n",s);
close(sfd);
close(nsfd);

//client.c

#include"header.h"
int main()
{
int sfd,len;
struct sockaddr_in server_id;
sfd=socket(AF_INET,SOCK_STREAM,0);
if(sfd<0)
{
perror("Socket");
return 0;
}
perror("Socket");

server_id.sin_family=AF_INET;
server_id.sin_port=htons(3000);
server_id.sin_addr.s_addr=inet_addr("[Link]");
len=sizeof(server_id);
if(connect(sfd,(struct
sockaddr*)&server_id,len)<0)
{
perror("connect");
return 0;
}
perror("connect");
char s[20];
printf("enter string from user\n");
scanf("%s",s);
write(sfd,s,strlen(s)+1); //writing data to server

close(sfd);

You might also like