1、简单server代码
/* File: server.c */
#include<stdio.h>
#include<stdlib.h>
#include<errno.h>
#include<string.h>
#include<sys/types.h>
#include<netinet/in.h>
#include<sys/socket.h>
#include<sys/wait.h>
#include<unistd.h>
#include<arpa/inet.h>
#include<sys/time.h>
#include<sys/types.h>
#include<resolv.h>
int main(int argc, char **argv)
{
int fd;
int address_len;
struct sockaddr_in address;
int listenfd;
//建立套接口
fd = socket(AF_INET, SOCK_STREAM, 0);
//绑定地址和端口
address.sin_family = AF_INET;
address.sin_addr.s_addr = htonl(INADDR_ANY);
address.sin_port = htons(1234);
address_len = sizeof(address);
bind(fd, (struct sockaddr *)&address, address_len);
//建立套接口队列
listenfd = listen(fd, 64);
while(1) {
struct sockaddr_in client_address;
int len;
int client_sockfd;
char *data = "Server to Client String!\n";
char data2[100];
pid_t pid;
printf("waiting...");
fflush(stdout);
//等待
len = sizeof(client_address);
client_sockfd = accept(fd, (struct sockaddr *)&client_address, &len);
//建立子进程
if( (pid = fork()) == 0){ //child
close(listenfd);
//接收数据
bzero((void *)data2, 100);
recv(client_sockfd, (void *)data2, 100, 0);
printf("server read line:%s", data2);
//发送数据
send(client_sockfd,(void *)data,strlen(data),0);
printf("server send line:%s", data);
//关闭联接并且退出
close(client_sockfd);
exit(0);
}
close(client_sockfd); //parent
}
}