Linux上的网络编程-初探

#include <stdio.h>

#include <string.h>

#include <unistd.h>

#include <stdlib.h>

#include <sys/types.h>

#include <sys/stat.h>

#include <fcntl.h>

#include <pthread.h>

#include <semaphore.h>

#include <wait.h>

#include <signal.h>

#include <sys/socket.h>

#include <arpa/inet.h>

#include <sys/ipc.h>

#include <sys/sem.h>

#include <semaphore.h>

#include <sys/msg.h>

#include <sys/shm.h>

#include <sys/un.h>

#include <sys/time.h>

#include <sys/select.h>

#include "doublyList.h"

#include "array.h"

myListOP * fdList;

struct info{

struct sockaddr_in client_addr;

int fd;

};

void printInt( int*a){

printf("%d",a);
}
struct info
creatInfo(struct sockaddr_in client_addr,int fd){
struct info
ret = malloc(sizeof(struct info));

if (client_addr)

{

ret->client_addr=*client_addr;

}

else{

memset(&ret->client_addr,0,sizeof(struct sockaddr_in));

}

复制代码
ret->fd = fd;
return ret;

}

void printInfo(struct info* info){

static char ip_human[16] = {0};

if (info ==0 || (&(info->client_addr)==0))

{

return;

}

复制代码
int port_human=ntohs(info->client_addr.sin_port);
inet_ntop(AF_INET,&info->client_addr.sin_addr,ip_human,16);
printf("ip:%s,port:%d,fd:%d::",ip_human,port_human,info->fd);

}

int serverfun(struct info* info){

int client = info->fd;

char ip_human[16]={0};

int port_human=0;

struct sockaddr_in client_addr = {0};

client_addr = info->client_addr;

inet_ntop(AF_INET,&client_addr.sin_addr,ip_human,16);

port_human=ntohs(client_addr.sin_port);

char buf[64] = "";

int res = read(client,buf,64);

if (res == 0)

{

printf("客户端已断开连接:\nIP:%s\nport:%d\n",ip_human,port_human);

close(client);

fdList->del(fdList,fdList->find(fdList,info,0));

return 0;

}

printf("客户端IP:%s\nport:%d\n发来消息:%s\n",ip_human,port_human,buf);

return 1;

}

int main(int argc, char const *argv[])

{

if (argc<2)

{

printf("请输入端口号\n");

return 1;

}

复制代码
int server = socket(AF_INET,SOCK_STREAM,0);
struct sockaddr_in addr={0};
addr.sin_family=AF_INET;
addr.sin_port=htons(atoi(argv[1]));
addr.sin_addr.s_addr=inet_addr("0.0.0.0");

if(bind(server,(struct sockaddr * )&addr,sizeof(addr))==-1){
    perror("bind:");
    return 1;
}
struct sockaddr_in client_addr = {0};
int client_len = sizeof(client_addr);

int ret;
int max = 10;
listen(server,max);
char ip_human[16]="";
int port_human = 0;
//创建监视列表
fd_set fd_listen;
FD_ZERO(&fd_listen);
int client;
fdList = myListDInit((void(*)(void*))printInt);
struct info * currentinfo=0;
int* currentfd =0;
unsigned int i=0;

fdList->append(fdList,creatInfo(0,server));

printf("服务器正在运行:\n");
for (;;)
{
    i=0;
    while (currentinfo = (struct info*)fdList->slice(fdList,i++))
    {
        currentfd = &currentinfo->fd;
        FD_SET(*currentfd,&fd_listen);//重置fd表
    }

    select(max,&fd_listen,0,0,0);//让服务器阻塞,等待
    //似乎有问题,select不一定有东西可以操作
    //解除阻塞时一定有东西可以操作
    i=0;
    while (currentinfo = fdList->slice(fdList,i++))
    {
        currentfd = &currentinfo->fd;
        if (*currentfd == server&&FD_ISSET(*currentfd,&fd_listen))//有新的客户端连接
        {
            

            client = accept(server,(struct sockaddr * )&client_addr,&client_len);

            fdList->append(fdList,creatInfo(&client_addr,client));

            inet_ntop(AF_INET,&client_addr.sin_addr,ip_human,16);
            port_human=ntohs(client_addr.sin_port);
            printf("客户端已连接:\nIP:%s\nport:%d\n",ip_human,port_human);
            continue;
        }
        //客户端操作
        else{
            serverfun(currentinfo);
        }
        
    }
    
    
    

}  
return 0;

}

相关推荐
JANYI201811 分钟前
嵌入式MCU和Linux开发哪个好?
linux·单片机·嵌入式硬件
熊大如如27 分钟前
Java NIO 文件处理接口
java·linux·nio
晚秋大魔王42 分钟前
OpenHarmony 开源鸿蒙南向开发——linux下使用make交叉编译第三方库——nettle库
linux·开源·harmonyos
农民小飞侠44 分钟前
ubuntu 24.04 error: cannot uninstall blinker 1.7.0, record file not found. hint
linux·运维·ubuntu
某不知名網友1 小时前
Linux 软硬连接详解
linux·运维·服务器
hnlucky1 小时前
通俗易懂版知识点:Keepalived + LVS + Web + NFS 高可用集群到底是干什么的?
linux·前端·学习·github·web·可用性测试·lvs
Jogging-Snail1 小时前
Linux工作台文件操作命令全流程解析(高级篇之vim和nano精讲)
linux·运维·vim·文件操作·文本编辑·nano
爱学习的章鱼哥1 小时前
计算机网络|| 常用网络命令的作用及工作原理
linux·服务器·计算机网络
zm1 小时前
网络编程epoll和udp
服务器·网络·数据库
张一不吃豆芽1 小时前
TCPIP详解 卷1协议 八 ICMPv4和ICMPv6 Internet控制报文协议
网络·网络协议·tcp/ip