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

Program 3

This document contains a basic implementation of a TCP client-server program in C. The server accepts a port number and IP address as command line arguments, binds to the specified address, listens for client connections, and receives a string sent by the client. The client also takes the server's port and IP address as command line arguments, connects to the server, and sends a user-input string.

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)
17 views8 pages

Program 3

This document contains a basic implementation of a TCP client-server program in C. The server accepts a port number and IP address as command line arguments, binds to the specified address, listens for client connections, and receives a string sent by the client. The client also takes the server's port and IP address as command line arguments, connects to the server, and sends a user-input string.

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 (client

sending string and server receiving data from


client,port number and ip address providing
through command line arguments)

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 argc,char **argv)
{
if(argc!=3)
{
printf("usage: ./server server_port
server_ip_address\n");
return 0;
}
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(atoi(argv[1]));
server_id.sin_addr.s_addr=inet_addr(argv[2]);
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 argc,char **argv)
{
if(argc!=3)
{
printf("usage: ./client server_port
server_ip_address\n");
return 0;
}
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(atoi(argv[1]));
server_id.sin_addr.s_addr=inet_addr(argv[2]);
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