0% found this document useful (0 votes)
80 views5 pages

TCP Client

This document contains code for a TCP client and server program written in C. The client code connects to a server running on localhost port 8088, allows the user to enter a message which is written to the server, and reads and prints the response. The server code binds to port 8000, accepts a connection, reads any data sent and prints it, allows user input of a response which is written back to the client. Sample output shows the client and server exchanging the messages "hi" and "hello".

Uploaded by

Vaibhav C Gandhi
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)
80 views5 pages

TCP Client

This document contains code for a TCP client and server program written in C. The client code connects to a server running on localhost port 8088, allows the user to enter a message which is written to the server, and reads and prints the response. The server code binds to port 8000, accepts a connection, reads any data sent and prints it, allows user input of a response which is written back to the client. Sample output shows the client and server exchanging the messages "hi" and "hello".

Uploaded by

Vaibhav C Gandhi
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/ 5

TCP Client

#include<stdio.h>

#include<netinet/in.h>

#include<sys/types.h>

#include<sys/socket.h>

#include<string.h>

main()

int rd,i;

char msg1[20],msg[20];

socklen_t l;

struct sockaddr_in server;

bzero(&server,sizeof(server));

server.sin_family=AF_INET;

server.sin_port=htons(8088);

inet_pton(AF_INET,"127.0.0.1",&server.sin_addr);

rd=socket(AF_INET,SOCK_STREAM,0);

if(rd==-1)

printf("error in connection");
}

l=sizeof(server);

connect(rd,(struct sockaddr *)&server,l);

while(1)

memset(msg1,'\0',20);

memset(msg,'\0',20);

printf("\n client");

scanf("%s",&msg);

write(rd,msg,20);

read(rd,msg1,20);

printf("%s\n",msg1);

return 0;

}
TCP server

#include<stdio.h>

#include<sys/types.h>

#include<sys/socket.h>

#include<netinet/in.h>

#include<netinet/ip.h>

#include<string.h>

main()

int n,sd,sd1,i;

char msg[20],msg1[20];

struct sockaddr_in server,client;

socklen_t len;

server.sin_family=AF_INET;

server.sin_port=htons(8000);

server.sin_addr.s_addr=htonl(INADDR_ANY);

sd=socket(AF_INET,SOCK_STREAM,0);

if(sd==-1)

printf("\nSocket not created");

i=bind(sd,(struct sockaddr *)&server,sizeof(server));

if(i==-1)
{

printf("\nBinding error occured:");

i=listen(sd,50);

len=sizeof(client);

sd1=accept(sd,(struct sockaddr *)&client,&len);

while(1)

memset(msg,'\0',50);

read(sd1,msg,50);

printf("%s",msg);

memset(msg1,'\0',20);

printf("\nserver");

scanf("%s",msg1);

write(sd1,msg1,20);

return 0;

}
Clent Output

student@suseserver:~> gcc ./client_tcp.c -o ./c

student@suseserver:~> ./c

client hi

hello

client

server Output

student@suseserver:~> gcc ./server_tcp.c -o ./s

student@suseserver:~> ./s

hi

server hello

You might also like