文章目录
linux服务端
linux客户端
start/stop/restart TCP服务
service服务
service服务(可打印信息)
查看进程
nohup后台执行
linux服务端
#include #include #include #include #include #include #include #include #include #include #define MYPORT 8887
#define QUEUE 20
#define BUFFER_SIZE 1024
int main()
{
int server_sockfd = socket(AF_INET,SOCK_STREAM, 0);
///sockaddr_in
struct sockaddr_in server_sockaddr;
server_sockaddr.sin_family = AF_INET;
server_sockaddr.sin_port = htons(MYPORT);
server_sockaddr.sin_addr.s_addr = htonl(INADDR_ANY);
///bind, success 0, error -1
if(bind(server_sockfd,(struct sockaddr *)&server_sockaddr,sizeof(server_sockaddr))==-1)
{
perror("bind");
exit(1);
}
printf("listen %d port\n",MYPORT);
///listen, success 0, error -1
if(listen(server_sockfd,QUEUE) == -1)
{
perror("listen");
exit(1);
}
///sockaddr
char buffer[BUFFER_SIZE];
struct sockaddr_in client_addr;
socklen_t length = sizeof(client_addr);
printf("waitting for client...\n");
///error -1
int conn = accept(server_sockfd, (struct sockaddr*)&client_addr, &length);
if(conn<0)
{
perror("connect");
exit(1);
}
printf("connect client successful!\n");
while(1)
{
memset(buffer,0,sizeof(buffer));
int len = recv(conn, buffer, sizeof(buffer),0);
//recv 'exit' or error
if(strcmp(buffer,"exit\n")==0 || len<=0)
break;
printf("Recv from client: %s\n",buffer);
send(conn, buffer, len, 0);
printf("Send to client: %s\n",buffer);
}
close(conn);
close(server_sockfd);
return 0;
}
linux客户端
#include #include #include #include #include #include #include #include #include #include #define MYPORT 8887
#define BUFFER_SIZE 1024
char* SERVER_IP = "10.247.219.47";
int main()
{
int sock_cli = socket(AF_INET,SOCK_STREAM, 0);
struct sockaddr_in servaddr;
memset(&servaddr, 0, sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_port = htons(MYPORT); ///service port
servaddr.sin_addr.s_addr = inet_addr(SERVER_IP); //service IP
printf("connect %s: %d\n",SERVER_IP,MYPORT);
//connect service, success 0, error -1
if (connect(sock_cli, (struct sockaddr *)&servaddr, sizeof(servaddr)) < 0)
{
perror("connect");
exit(1);
}
printf("connect service successful!\n");
char sendbuf[BUFFER_SIZE];
char recvbuf[BUFFER_SIZE];
while (fgets(sendbuf, sizeof(sendbuf), stdin) != NULL)
{
printf("send to service: %s\n",sendbuf);
send(sock_cli, sendbuf, strlen(sendbuf),0);
if(strcmp(sendbuf,"exit\n")==0)
break;
recv(sock_cli, recvbuf, sizeof(recvbuf),0);
printf("recv from service: %s\n",recvbuf);
memset(sendbuf, 0, sizeof(sendbuf));
memset(recvbuf, 0, sizeof(recvbuf));
}
close(sock_cli);
return 0;
}
start/stop/restart TCP服务
新建start.sh,stop.sh,restart.sh脚本。
start
#!/bin/sh
./$1/$2
if [[ $(pidof $2) ]] ;then
echo "start $1 success"
else
echo "start $1 faileds"
fi
stop
#!/bin/sh
while [[ $(pidof $1) ]]
do
kill -TERM $(pidof $1)
done
restart
#!/bin/sh
while [[ $(pidof $2) ]]
do
kill -TERM $(pidof $2)
done
./$1/$2
if [[ $(pidof $2) ]] ;then
echo "reload $2 success"
else
echo "reload $2 faieds"
fi
service服务
新建文件/usr/lib/systemd/system/run.service
启动、关闭、重启服务:systemctl start/stop/restart run
是否启动、关闭成功只能通过查看进程知道,不能添加打印信息。
simple方式后台运行
forking方式会在等待响应处等待
[Unit]
Description=TCPIP
After=network.target remote-fs.target nss-lookup.target
[Service]
Type=simple
ExecStart=/usr/bin/sh /home/zhangquan/TCP_SERVICE/start.sh /home/zhangquan/TCP_SERVICE/ TCP_Service
ExecReload=/usr/bin/sh /home/zhangquan/TCP_SERVICE/restart.sh /home/zhangquan/TCP_SERVICE/ TCP_Service
ExecStop=/usr/bin/sh /home/zhangquan/TCP_SERVICE/stop.sh TCP_Service
[Install]
WantedBy=multi-user.target
service服务(可打印信息)
上面是通过service文件启动可执行文件,还可以通过添加&符号使程序后台运行
./TCP_Service &
新建文件/etc/init.d/myService
chmod a+x myService
启动/重启/关闭服务:service myService start/stop/restart
start() {
echo "Starting the TCP Service..."
./home/zhangquan/TCP_SERVICE/TCP_Service &
}
stop() {
echo "Stopping the TCP Service..."
if [ $(pidof TCP_Service) ] ;then
kill -TERM $(pidof TCP_Service)
fi
}
restart() {
echo "Restarting the TCP Srevice..."
}
case "$1" in
start)
start
if [ $(pidof TCP_Service) ] ;then
echo "Start the TCP Service successfully!"
else
echo "Start the TCP Service failed!"
fi
;;
stop)
stop
if [ $(pidof TCP_Service) ] ;then
echo "Stop the TCP Service failed!"
ps aux | grep TCP_Service
else
echo "Stop the TCP Service successfully!"
fi
;;
restart)
stop
start
if [ $(pidof TCP_Service) ] ;then
echo "Restart the TCP Service successfully!"
else
echo "Restart the TCP Service failed!"
fi
;;
*)
esac
查看进程
ps -aux | grep TCP_Service
nohup后台执行
#会停留在等待响应处
nohup ./TCP_Service &
#不会停留
nohup ./TCP_Service > log.txt &