TCP socket communication examples client and Server

//server.c

#include <stdio.h>

#include <sys/types.h>

#include <sys/socket.h>

#include <netinet/in.h>

#include <arpa/inet.h>

#define BUFFSIZE 4096

int main(int argc, char *argv\[\])

{

const char* szwelcomestr="Welcome to my configure server.";

int server_sockfd;//server socket

int client_sockfd;//client socket

int len;

struct sockaddr_in my_addr; //server addr

struct sockaddr_in remote_addr; //client addr

int sin_size;

char bufBUFFSIZE; //buff to save data

int nflag=1;

memset(&my_addr,0,sizeof(my_addr)); //zero

my_addr.sin_family=AF_INET; //ip family

my_addr.sin_addr.s_addr= inet_addr("192.168.1.34");//binding server ip

my_addr.sin_port=htons(8000); //server port

/*create server socket*/

if((server_sockfd=socket(PF_INET,SOCK_STREAM,0))<0)

{

perror("socket");

return 1;

}

/*bind*/

if (bind(server_sockfd,(struct sockaddr *)&my_addr,sizeof(struct sockaddr))<0)

{

perror("bind");

return 1;

}

/*listen num is 5*/

listen(server_sockfd,5);

sin_size=sizeof(struct sockaddr_in);

/*accept ,wait client to arrive at*/

if((client_sockfd=accept(server_sockfd,(struct sockaddr *)&remote_addr,&sin_size))<0)

{

perror("accept");

return 1;

}

printf("accept client %s\n",inet_ntoa(remote_addr.sin_addr));

len=send(client_sockfd,szwelcomestr,strlen(szwelcomestr),0);//send the welcome information

/*receive the data from client, and send back to it*/

while((len=recv(client_sockfd,buf,BUFFSIZE,0)) > 0)

{

printf("reve data %s,len=%d\n",buf, len);

if(send(client_sockfd,buf,len,0)<0)

{

perror("write");

return 1;

}

//clear the buff

memset(buf, 0, sizeof(buf));

}

close(client_sockfd);

close(server_sockfd);

return 0;

}

//client.c

#include <stdio.h>

#include <sys/types.h>

#include <sys/socket.h>

#include <netinet/in.h>

#include <arpa/inet.h>

int main(int argc, char *argv\[\])

{

int client_sockfd;

int len;

struct sockaddr_in remote_addr; //server address

char bufBUFSIZ; //buff definition

memset(&remote_addr,0,sizeof(remote_addr)); //set zero

remote_addr.sin_family=AF_INET; //IPfamily

remote_addr.sin_addr.s_addr=inet_addr("192.168.1.34");//server IP to link

remote_addr.sin_port=htons(8000); //server port to link

/*create the socket for tcp stream*/

if((client_sockfd=socket(PF_INET,SOCK_STREAM,0))<0)

{

perror("socket");

return 1;

}

/*collect to the server socket*/

if(connect(client_sockfd,(struct sockaddr *)&remote_addr,sizeof(struct sockaddr))<0)

{

perror("connect");

return 1;

}

printf("connected to server \n");

len=recv(client_sockfd,buf,BUFSIZ,0);//receive the server data

buflen='\0';

printf("%s",buf); //print the server welcom banner

/*loop the send the string and print the received data, exit to quit the loop*/

while(1)

{

printf("Enter string to send:");

scanf("%s",buf);

if(strcmp(buf,"exit") ==0)

break;

printf("client to send %s,length of buf is %d\n", buf,strlen(buf));

len=send(client_sockfd,buf,strlen(buf),0);

len=recv(client_sockfd,buf,BUFSIZ,0);

buflen='\0';

printf("received:%s\n",buf);

}

close(client_sockfd);//close the socket

return 0;

}

相关推荐
en.en..1 小时前
MRS/MSR 指令操作 xPSR 寄存器
linux·运维·服务器
总裁余(余登武)1 小时前
K-means算法改进(Python版)
python·算法·kmeans
灯澜忆梦2 小时前
【面向对象编程c++】| 指针与自定义数据类型
开发语言·c++·算法
Brilliantwxx2 小时前
【Linux】 自主shell编写巩固复习篇
linux·运维·服务器·开发语言
anew___2 小时前
《从零手写操作系统 (48):网络协议栈基础——Socket API与TCP/IP分层架构》
网络协议·tcp/ip·架构
鱼很腾apoc2 小时前
【MySQL】第3期 索引、事务与视图系统详解
linux·mysql·事务·索引·dml·视图·macc
xxwl5852 小时前
区域赛算法模版第八章---贪心(Greedy)
c++·笔记·学习·算法
anew___2 小时前
《从零手写操作系统 (49):虚拟内存进阶——Demand Paging、Swap与缺页异常处理》
linux·服务器·php
TMT星球2 小时前
2026阅文创作大会:首批AI精品动画亮相,推出OPC一站式平台与IP委员会模式
人工智能·网络协议·tcp/ip