server.c
资源名称:server.rar [点击查看]
上传用户:huangiiw
上传日期:2022-08-02
资源大小:1k
文件大小:1k
源码类别:
网络编程
开发平台:
Unix_Linux
- #include <sys/types.h>
- #include <sys/socket.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <errno.h>
- #include <string.h>
- #include <unistd.h>
- #include <netinet/in.h>
- #define PORT 4321
- #define BUFFER_SIZE 1024
- #define MAX_QUE_CONN_NM 1
- int main()
- {
- struct sockaddr_in server_sockaddr,client_sockaddr;
- int sin_size,recvbytes;
- int sockfd,client_fd;
- double buf[BUFFER_SIZE];
- /*建立socket连接*/
- if ((sockfd=socket(AF_INET,SOCK_STREAM,0))==-1)
- {
- perror("socket");
- exit(1);
- }
- printf("socket id = %dn",sockfd);
- server_sockaddr.sin_family=AF_INET;
- server_sockaddr.sin_port=htons(PORT);
- server_sockaddr.sin_addr.s_addr=INADDR_ANY;
- bzero(&(server_sockaddr.sin_zero),8);
- int i=1;
- setsockopt(sockfd,SOL_SOCKET,SO_REUSEADDR,&i,sizeof(i));
- if(bind(sockfd,(struct sockaddr *)&server_sockaddr,sizeof(struct sockaddr))==-1)
- {
- perror("bind");
- exit(1);
- }
- printf("bind success!n");
- if(listen(sockfd,MAX_QUE_CONN_NM)==-1)
- {
- perror("listen");
- exit(1);
- }
- printf("listening......n");
- if((client_fd=accept(sockfd,(struct sockaddr *)&client_sockaddr,&sin_size))==-1)
- {
- perror("accept");
- exit(1);
- }
- while(1) {
- memset(buf,0,sizeof(buf));
- if ((recvbytes=recv(client_fd,buf,BUFFER_SIZE,0))==-1)
- {
- perror("recv");
- exit(1);
- }
- printf("received success!n"); for(i=0;i<15;i++) printf("the data is %en",buf[i]);
- // close(sockfd); }
- exit(0);
- }