//刚开始淦网络编程 加油!!! 记录一下0.0
服务端代码:
/*************************************************************************
> File Name: server.c
> Author: ma6174
> Mail: ma6174@163.com
> Created Time: 2020年08月01日 星期六 16时54分24秒
************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <unistd.h>
#include <arpa/inet.h>
#define N 128
int main(int argc, char const *argv[])
{
if(argc < 3) {
perror("input ip and port\n");
exit(1);
}
int sockfd;
if((sockfd = socket(AF_INET,SOCK_STREAM,0)) == -1)
{
fprintf(stdout,"Create socket error\n");
exit(1);
}
//sockaddr
struct sockaddr_in server_addr; //设置地址结构
server_addr.sin_family = AF_INET;
server_addr.sin_addr.s_addr = inet_addr(argv[1]);
server_addr.sin_port = htons(atoi(argv[2]));
int val = 1; //加上这个能很好的解决端口复用的问题
int ret = setsockopt(sockfd,SOL_SOCKET,SO_REUSEADDR,(void *)&val,sizeof(int));
if(ret == -1)
{
printf("setsockopt");
exit(1);
}
//bind
if(bind(sockfd,(struct sockaddr*)&server_addr,sizeof(server_addr)) == -1)
{
perror("bind error\n");
exit(1);
}
if(listen(sockfd,128) == -1)
{
perror("linten error\n");
exit(1);
}
//accept
struct sockaddr_in client_addr; //存放客户端的相关信息
socklen_t len = sizeof(client_addr);
int recv_fd;
if((recv_fd = accept(sockfd,(struct sockaddr*)&client_addr,&len)) == -1)
{
perror("accept error\n");
exit(1);
}
char buf[N];
while(1)
{
//read
memset(buf,'\0',sizeof(buf));
int ret;
if((ret = read(recv_fd,buf,sizeof(buf))) == -1)
{
perror("fail ti read\n");
exit(1);
}
char s[N];
memset(s,'\0',sizeof(s));
printf("[ip : %s -- port : %d]\n",inet_ntop(AF_INET,&client_addr.sin_addr.s_addr,s,sizeof(s)),
ntohs(client_addr.sin_port));
printf("Accept : %s\n",buf);
write(recv_fd,buf,ret);
if(memcmp(buf,"###",3) == 0) //当接收三个#表示结束退出循环
break;
}
close(sockfd);
close(recv_fd);
return 0;
}
多进程并发:
```c
signal(SIGCHLD,SIG_IGN); //防止僵尸进程
while(1)
{
struct sockaddr_in client_addr;
socklen_t clen = sizeof(client_addr);
int cfd;
cfd = accept(sfd,(struct sockaddr*)&client_addr,&clen);
if(cfd == -1) sys_err("accept error");
int pid;
pid = fork();
if(pid == -1) sys_err("fork error");
if(pid == 0){ //子进程负责接收数据
close(sfd); //关闭监听 只接收
while(1)
{
memset(buf,'\0',sizeof(buf));
if(read(cfd,buf,N) != -1)
printf("[ip : %s -- port : %d] Data : %s\n",inet_ntoa(client_addr.sin_addr),
ntohs(client_addr.sin_port),buf);
else break;
write(cfd,"Accept",7);
}
}
else
{
close(cfd); //父进程负责监听 关闭接收
}
}
客户端代码:
```c
/*************************************************************************
> File Name: client.c
> Author: ma6174
> Mail: ma6174@163.com
> Created Time: 2020年08月01日 星期六 17时39分40秒
************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <unistd.h>
#include <arpa/inet.h>
#define N 128
int main(int argc, char const *argv[])
{
if(argc < 3) {
perror("input ip and port\n");
exit(1);
}
int sockfd;
if((sockfd = socket(AF_INET,SOCK_STREAM,0)) == -1)
{
fprintf(stdout,"Create socket error\n");
exit(1);
}
struct sockaddr_in client_addr;
client_addr.sin_family = AF_INET;
client_addr.sin_addr.s_addr = inet_addr(argv[1]);
client_addr.sin_port = htons(atoi(argv[2]));
// bind(sockfd,(struct sockaddr *)&client_addr,sizeof(client_addr));
//connect
if(connect(sockfd,(struct sockaddr*)&client_addr,sizeof(client_addr)) == -1)
{
perror("connect error\n");
exit(1);
}
char buf[N];
char recv_buf[N];
memset(buf,'\0',sizeof(buf));
while(fgets(buf,N,stdin) != NULL)
{
memset(recv_buf,'\0',sizeof(recv_buf));
write(sockfd,buf,strlen(buf));
read(sockfd,recv_buf,sizeof(recv_buf));
printf("server : %s\n",recv_buf);
if(memcmp("###",recv_buf,3) == 0) break;
memset(buf,'\0',sizeof(buf));
}
close(sockfd);
return 0;
}
Makefile:
CC = gcc
CFLAGS = -lm -Wall -g
all:server client
server:server.o
$(CC) $(CFLAGS) server.o -o server
client:client.o
$(CC) $(CFLAGS) client.o -o client
server.o:server.c
$(CC) $(CFLAGS) -c server.c -o server.o
client.o:client.c
$(CC) $(CFLAGS) -c client.c -o client.o
clear:
rm *.o
运行结果: