linux下TCP Socket编程C语言小实例

时间:2021-07-12 05:19:47

  几乎是全部按照这个链接上的网址实现的:http://blog.csdn.net/leo115/article/details/8044795


  服务器端的代码:

<span style="font-size:18px;">#include <stdio.h>
#include <stdlib.h>
#include <strings.h>
#include <sys/socket.h>
#include <memory.h>
#include <unistd.h>
#include <netinet/in.h>
#include <arpa/inet.h>

//处理sighandler_t没有定义的问题
#include <signal.h>
typedef void (*sighandler_t)(int);
sighandler_t signal(int sig, sighandler_t handler);

#define PORT 11910 //定义通信的端口号
#define BACKLOG 5
#define buflen 1024



void process_conn_server(int s); //跟客户端进行通信的处理函数
void sig_pipe(int signo); //信号处理函数

int ss, sc;

int main(int argc, char *argv[])
{
//客户端和服务器端的地址结构
struct sockaddr_in server_addr;
struct sockaddr_in client_addr;

int err;
pid_t pid;

ss = socket(AF_INET, SOCK_STREAM, 0); //建立一个套接字,TCP
if(ss < 0){
printf("server: server socket create error\n");
return -1;
}

//注册信号
sighandler_t ret;
ret = signal(SIGTSTP, sig_pipe);
if(SIG_ERR == ret){
printf("signal mount error\n");
return -1;
}
else
printf("signal mount ok\n");

//初始化地址结构
memset(&server_addr, 0, sizeof(server_addr));
server_addr.sin_family = AF_INET;
server_addr.sin_addr.s_addr = htonl(INADDR_ANY); //设置地址
server_addr.sin_port = htons(PORT); //设置端口号

//将套接字和地址绑定
err = bind(ss, (struct sockaddr *)&server_addr, sizeof(struct sockaddr));
if(err < 0){
printf("server : bind error\n");
return -1;
}

//设置监听队列的大小
err = listen(ss, BACKLOG);
if(err < 0){
printf("server : listen error\n");
return -1;
}

for(;;){
socklen_t addrlen = sizeof(client_addr);
//sc为客户端套接字描述符
sc = accept(ss, (struct sockaddr *)&client_addr, &addrlen);
if(sc < 0) continue;
else printf("server : connected\n");


pid = fork();
if(pid == 0){ //子进程与客户端进行通信
close(ss);
process_conn_server(sc);
}
else close(sc); //客户端继续进行监听
}
}

void process_conn_server(int s)
{
ssize_t size = 0;
char buffer[buflen];

for(;;){
//收到从客户端发来的数据并进行输出
for(size = 0; size == 0; size = read(s, buffer, buflen));
printf("%s", buffer);

if(strcmp(buffer, "quit") == 0){
close(s);
return;
}
sprintf(buffer, "%d bytes altogether\n", size);
write(s, buffer, strlen(buffer) + 1);
}
}

void sig_pipe(int signo)
{
printf("catch a signal\n");
if(signo == SIGTSTP){
printf("get SIGTSTP signal");
int ret1 = close(ss);
int ret2 = close(sc);
int ret = ret1 > ret2 ? ret1 : ret2;
if(ret == 0) printf("success : close IPC");
else if(ret == -1) printf("fail : not close IPC");

exit(1);
}
}
</span>


  客户端程序:
<span style="font-size:18px;">#include <stdio.h>#include <strings.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <stdlib.h>
#include <memory.h>
#include <arpa/inet.h>
#include <netinet/in.h>

#include <signal.h>
typedef void (*sighandler_t)(int);
sighandler_t signal(int sig, sighandler_t handler);

#define PORT 11910 //端口号
#define Buflen 1024

void process_conn_client(int s); //客户端进行通信的处理函数
void sig_pipe(int signo);

int s;

int main(int argc, char *argv[])
{
struct sockaddr_in server_addr; //服务器端的地址结构
int err;
sighandler_t ret;

char server_ip[50] = "";

s = socket(AF_INET, SOCK_STREAM, 0); //建立套接字
if(s < 0){
printf("client : create socket error\n");
return 1;
}

ret = signal(SIGTSTP, sig_pipe);
if(SIG_ERR == ret){
printf("signal mount fail\n");
return -1;
}
else printf("signal mount success\n");

//初始化服务器端的地址结构
memset(&server_addr, 0, sizeof(server_addr));
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(PORT);
server_addr.sin_addr.s_addr = htonl(INADDR_ANY);

//输入服务器的ip地址
printf("please input server ip address: \n");
read(0, server_ip, 50);
server_addr.sin_addr.s_addr = inet_addr(server_ip);

//与服务器端进行连接操作
err = connect(s, (struct sockaddr *)&server_addr, sizeof(struct sockaddr));
if(err == 0) printf("client : connect to server\n");
else{
printf("client : connect error\n");
return -1;
}

process_conn_client(s);
close(s);
}

void process_conn_client(int s)
{
ssize_t size = 0;
char buffer[Buflen];

for(;;){
memset(buffer, '\0', Buflen);
size = read(0, buffer, Buflen);
if(size > 0){
write(s, buffer, strlen(buffer) + 1);
for(size = 0; size == 0; size = read(s, buffer, Buflen));

write(1, buffer, strlen(buffer) + 1);
}
}
}

void sig_pipe(int signo)
{
printf("Catch a signal\n");
if(signo == SIGTSTP){
int ret = close(s);
if(ret == 0) printf("success : close IPC\n");
else if(ret == -1) printf("fail : not close IPC\n");
exit(1);
}
}</span>

  时间有点晚了,要回宿舍,写的比较草,准备明天自己实现一个,客户端输入两个整数,服务器端实现完成两个整数的加法,尝试一下...