/*----------------------------tcp_client.c----------------------------------------*/
#include<stdio.h>
#include<sys/socket.h>
#include<sys/types.h>
#include<netinet/in.h>
#include<string.h>
#define LOCAL_PORT 1500
#define SIZE 20
int main()
{
int sd,cd,len;
char buff1[SIZE],buff[SIZE];
struct sockaddr_in server,client;
sd = socket(AF_INET,SOCK_STREAM,0);
client.sin_family = AF_INET;
client.sin_port = htons(LOCAL_PORT);
client.sin_addr.s_addr = inet_addr("127.0.0.1");
bind(sd,(struct sockaddr *)&client,sizeof(client));
server.sin_family = AF_INET;
server.sin_port = htons(LOCAL_PORT);
server.sin_addr.s_addr = inet_addr("127.0.0.1");
connect(sd,(struct sockaddr *)&server,sizeof(server));
while(1)
{
/*Sending to server*/
printf("Enter the data:");
scanf("%s",buff1);
printf("Data:-%s",buff1);
write(sd,buff1,20);
printf("\n");
/*Receiving from server*/
read(cd,buff,20);
printf("Data:- %s\n",buff);
}
close(sd);
return 0;
}
/*----------------------------tcp_server.c----------------------------------------*/
#include<stdio.h>
#include<sys/socket.h>
#include<sys/types.h>
#include<netinet/in.h>
#define LOCAL_PORT 1500
int main()
{
int sd,cd,buff[20],cliLen;
char buff1[20];
struct sockaddr_in server,client;
sd = socket(AF_INET,SOCK_STREAM,0);
server.sin_family = AF_INET;
server.sin_port = htons(LOCAL_PORT);
server.sin_addr.s_addr = inet_addr("127.0.0.1");
bind(sd,(struct sockaddr *)&server,sizeof(server));
listen(sd,5);
cliLen = sizeof(client);
cd = accept(sd,(struct sockaddr *)&client,&cliLen);
while(1)
{
/*Receiving from client*/
read(cd,buff,20);
printf("Data:- %s\n",buff);
/*Sending to client*/
printf("Enter the data:");
scanf("%s",buff1);
// printf("Data:-%s",buff1);
write(cd,buff1,20);
printf("\n");
}
close(cd);
close(sd);
return 0;
}