#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define PORT 2345
#define MAXSIZE 1024
int main(int argc, char *argv[])
{
int i=1,h=2;
printf("i + h = %d\n", i+h);
int sockfd, newsockfd;
//定义服务端套接口数据结构
struct sockaddr_in server_addr;
struct sockaddr_in client_addr;
int sin_size, portnumber;
//发送数据缓冲区
char buf[MAXSIZE];
//定义客户端套接口数据结构
int addr_len = sizeof(struct sockaddr_in);
//创建socket接口
//协议族 AF_INET:TCP/IP – IPv4
if((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0){
fprintf(stderr, "create socket failed\n");
exit(EXIT_FAILURE);
}
puts("create socket success");
printf("socket is %d\n", sockfd);
//清空表示地址的结构体变量
bzero(&server_addr, sizeof(struct sockaddr_in));
//设置addr的成员变量信息
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(PORT);
//设置ip为本机ip
server_addr.sin_addr.s_addr = htonl(INADDR_ANY);
//绑定端口
if(bind(sockfd, (struct sockaddr*)(&server_addr), sizeof(struct sockaddr)) < 0){
fprintf(stderr, "bind failed\n");
exit(EXIT_FAILURE);
}
puts("bind success\n");
//监听端口
if(listen(sockfd, 10) < 0){
perror("listen fail\n");
exit(EXIT_FAILURE);
}
puts("listen successs\n");
sin_size = sizeof(struct sockaddr_in);
printf("sin_size is %d\n", sin_size);
//接收,等待连接
if((newsockfd = accept(sockfd, (struct sockaddr *)(&client_addr), &sin_size)) < 0){
perror("accept error");
exit(EXIT_FAILURE);
}
printf("accept a new connetction\n");
printf("new socket id is %d\n", newsockfd);
printf("Accept cilent ip is %s\n", inet_ntoa(client_addr.sin_addr));
printf("Connect successful please input message\n");
char mybuf[1024];
char sendbuf[1024];
while(1){
int len = recv(newsockfd, mybuf, sizeof(mybuf), 0);
//strcmp比较字符串
if(strcmp(mybuf, "exit\n") == 0) break;
//输出buf中数据
fputs(mybuf, stdout);
send(newsockfd, mybuf, len, 0);
memset(sendbuf, 0, sizeof(sendbuf));
memset(mybuf, 0, sizeof(mybuf));
}
close(newsockfd);
close(sockfd);
puts("exit success");
exit(EXIT_SUCCESS);
return 0;
}
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define PORT 2345
int count = 1;
int main()
{
int sockfd;
char buffer[2014];
struct sockaddr_in server_addr;
struct hostent *host;
int nbytes;
//创建socket
if((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1){
fprintf(stderr, "Socket Error is %s\n", strerror(errno));
exit(EXIT_FAILURE);
}
//清空表示地址的结构体变量
bzero(&server_addr, sizeof(server_addr));
//设置addr的成员变量信息
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(PORT);
//设置ip为本机ip
server_addr.sin_addr.s_addr = htonl(INADDR_ANY);
//客户端发出请求
if(connect(sockfd, (struct sockaddr *)(&server_addr), sizeof(struct sockaddr)) == -1){
fprintf(stderr, "Connect failed\n");
exit(EXIT_FAILURE);
}
char sendbuf[1024];
char recvbuf[2014];
while(1){
// fgets(sendbuf, sizeof(sendbuf), stdin);
// send(sockfd, sendbuf, strlen(sendbuf), 0);
// if(strcmp(sendbuf, "exit\n") == 0) break;
// recv(sockfd, recvbuf, sizeof(recvbuf), 0);
// fputs(recvbuf, stdout);
// memset(sendbuf, 0, sizeof(sendbuf));
// memset(recvbuf, 0, sizeof(recvbuf));
fgets(sendbuf, sizeof(sendbuf), stdin);
send(sockfd, sendbuf, strlen(sendbuf), 0);
//fputs(sendbuf, stdout);
if(strcmp(sendbuf, "exit\n") == 0) break;
recv(sockfd, recvbuf, sizeof(recvbuf), 0);
fputs(recvbuf, stdout);
memset(sendbuf, 0, sizeof(sendbuf));
memset(recvbuf, 0, sizeof(recvbuf));
}
close(sockfd);
exit(EXIT_SUCCESS);
return 0;
}
参考博客:(4条消息) linux网络编程之用socket实现简单客户端和服务端的通信(基于TCP)_kunkliu的博客-CSDN博客