从客户端向服务器端发送信息时,在服务器端有打印显示;
检测环境常用,备份一下
0,公共头文件代码
//config.h
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <unistd.h>
#include <errno.h>
#include <netinet/in.h>
#include <netdb.h>
#include <arpa/inet.h>
const int MAX_LINE = 4096;
const int PORT = 6000;
const int BACKLOG = 10;
const int LISTENQ = 6666;
const int MAX_CONNECT = 20;
1,服务器代码
/*
* server.c
*/
#include "config.h"
int main(int argc , char **argv)
{
/*声明服务器地址和客户链接地址*/
struct sockaddr_in servaddr , cliaddr;
/*声明服务器监听套接字和客户端链接套接字*/
int listenfd , connfd;
pid_t childpid;
/*声明缓冲区*/
char buf[MAX_LINE];
socklen_t clilen;
/*(1) 初始化监听套接字listenfd*/
if((listenfd = socket(AF_INET , SOCK_STREAM , 0)) < 0)
{
perror("socket error");
exit(1);
}//if
/*(2) 设置服务器sockaddr_in结构*/
bzero(&servaddr , sizeof(servaddr));
servaddr.sin_family = AF_INET;
//servaddr.sin_addr.s_addr = htonl(INADDR_ANY); //表明可接受任意IP地址
servaddr.sin_addr.s_addr = inet_addr("10.20.30.40");//此处服务器本机ip,可以换成上一行
servaddr.sin_port = htons(PORT);
/*(3) 绑定套接字和端口*/
if(bind(listenfd , (struct sockaddr*)&servaddr , sizeof(servaddr)) < 0)
{
perror("bind error");
exit(1);
}//if
/*(4) 监听客户请求*/
if(listen(listenfd , LISTENQ) < 0)
{
perror("listen error");
exit(1);
}//if
/*(5) 接受客户请求*/
for( ; ; )
{
clilen = sizeof(cliaddr);
if((connfd = accept(listenfd , (struct sockaddr *)&cliaddr , &clilen)) < 0 )
{
perror("accept error");
exit(1);
}//if
//新建子进程单独处理链接
if((childpid = fork()) == 0)
{
close(listenfd);
//str_echo
ssize_t n;
char buff[MAX_LINE];
while((n = read(connfd , buff , MAX_LINE)) > 0)
{
write(connfd , buff , n);
printf("rec: %s", buff);
}
exit(0);
}//if
close(connfd);
}//for
/*(6) 关闭监听套接字*/
close(listenfd);
}
2,客户端代码
/*
* client.c
*/
#include "config.h"
/*readline函数实现*/
ssize_t readline(int fd, char *vptr, size_t maxlen)
{
ssize_t n, rc;
char c, *ptr;
ptr = vptr;
for (n = 1; n < maxlen; n++) {
if ( (rc = read(fd, &c,1)) == 1) {
*ptr++ = c;
if (c == '\n')
break; /* newline is stored, like fgets() */
} else if (rc == 0) {
*ptr = 0;
return(n - 1); /* EOF, n - 1 bytes were read */
} else
return(-1); /* error, errno set by read() */
}
*ptr = 0; /* null terminate like fgets() */
return(n);
}
int main(int argc , char ** argv)
{
/*声明套接字和链接服务器地址*/
int sockfd;
struct sockaddr_in servaddr;
/*判断是否为合法输入*/
if(argc != 2)
{
perror("usage:tcpcli <IPaddress>");
exit(1);
}//if
/*(1) 创建套接字*/
if((sockfd = socket(AF_INET , SOCK_STREAM , 0)) == -1)
{
perror("socket error");
exit(1);
}//if
/*(2) 设置链接服务器地址结构*/
bzero(&servaddr , sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_port = htons(PORT);
if(inet_pton(AF_INET , argv[1] , &servaddr.sin_addr) < 0)
{
printf("inet_pton error for %s\n",argv[1]);
exit(1);
}//if
/*(3) 发送链接服务器请求*/
if( connect(sockfd , (struct sockaddr *)&servaddr , sizeof(servaddr)) < 0)
{
perror("connect error");
exit(1);
}//if
/*(4) 消息处理*/
char sendline[MAX_LINE] , recvline[MAX_LINE];
while(fgets(sendline , MAX_LINE , stdin) != NULL)
{
write(sockfd , sendline , strlen(sendline));
if(readline(sockfd , recvline , MAX_LINE) == 0)
{
perror("server terminated prematurely");
exit(1);
}//if
if(fputs(recvline , stdout) == EOF)
{
perror("fputs error");
exit(1);
}//if
}//while
/*(5) 关闭套接字*/
close(sockfd);
}
3,构建代码
Makefile
all: client server
client: client.c
server:server.c
.PHONY:clean
clean:
-rm -f server client
4,效果
启动server:
$./server
启动client:
$./client 10.20.30.40