tcp client
TCP CLIENT PROGRAM
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<arpa/inet.h>
int main()
{
int sfd = socket(AF_INET,SOCK_STREAM,0);
if(sfd == -1)
{
printf("socket error : ");
perror("socket error : ");
exit(1);
}
else
{
printf("socket created successfull sfd : %d\n",sfd);
struct sockaddr_in server,clinet;
server.sin_family = AF_INET;
server.sin_port = htons( 8000);
server.sin_addr.s_addr = inet_addr("192.168.0.122");
int c = connect(sfd,(struct sockaddr *)&server,sizeof(server));
if(c == -1)
{
perror("bind failed :");
exit(1);
}
else
{
printf("Bind successful\n");
while(1)
{
char buff[50];
printf("enter a msg to server : \n");
fgets(buff,50,stdin);
int w = write(sfd,buff,sizeof(buff));
if(w == -1)
{
perror("write failed:");
exit(1);
}
else
{
printf("send successfully\n");
}
printf("waiting for msg from sever : \n");
// char buff[50];
int r = read(sfd,buff,sizeof(buff));
if(r == -1)
{
perror("read failed:");
exit(1);
}
else
{
printf("message read : %s\n",buff);
}
}
}
exit(0);
}
}
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<arpa/inet.h>
int main()
{
int sfd = socket(AF_INET,SOCK_STREAM,0);
if(sfd == -1)
{
printf("socket error : ");
perror("socket error : ");
exit(1);
}
else
{
printf("socket created successfull sfd : %d\n",sfd);
struct sockaddr_in server,clinet;
server.sin_family = AF_INET;
server.sin_port = htons( 8000);
server.sin_addr.s_addr = inet_addr("192.168.0.122");
int c = connect(sfd,(struct sockaddr *)&server,sizeof(server));
if(c == -1)
{
perror("bind failed :");
exit(1);
}
else
{
printf("Bind successful\n");
while(1)
{
char buff[50];
printf("enter a msg to server : \n");
fgets(buff,50,stdin);
int w = write(sfd,buff,sizeof(buff));
if(w == -1)
{
perror("write failed:");
exit(1);
}
else
{
printf("send successfully\n");
}
printf("waiting for msg from sever : \n");
// char buff[50];
int r = read(sfd,buff,sizeof(buff));
if(r == -1)
{
perror("read failed:");
exit(1);
}
else
{
printf("message read : %s\n",buff);
}
}
}
exit(0);
}
}
Comments
Post a Comment