赞
踩
服务器程序:
1. 创建一个socket,用函数socket()
2. 绑定IP地址、端口等信息到socket上,用函数bind()
3.设置允许的最大连接数,用函数listen()
4.接收客户端上来的连接,用函数accept()
5.收发数据,用函数send()和recv(),或者read()和write()
6.关闭网络连接
- #include<stdio.h>
- #include<sys/types.h>
- #include<sys/socket.h>
- #include<stdlib.h>
- #include<netinet/in.h>
- #include<arpa/inet.h>
- #include<string.h>
- #include<pthread.h>
- #define PORT 8000
- void *ClientHandler(void *arg)
- {
- pthread_detach(pthread_self()) ; //线程分离
- int ret;
- int fd=*(int *)arg;
- char buf[32]={0};
-
- ret=recv(fd,buf,sizeof(buf),0); //4个参数
- if(-1==ret)
- {
- perror("recv");
- // exit(1);
- }
- printf("recv from %d client %s!\n",fd,buf);
- memset(buf,0,sizeof(buf));
- }
-
- int main()
- {
- int sockfd;
- struct sockaddr_in server_addr;
- struct sockaddr_in client_addr;
- int fd[1000]&
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。