当前位置: 技术问答>linux和unix
初学网络编程,大家帮忙看看
来源: 互联网 发布时间:2015-09-21
本文导语: 服务器端: /*server.c*/ /* */ /* multithread tcp server */ /* uestc 2005/04/08 */ #include #include #include /*for bzero()*/ #include #include #include #include /*for close()*/ #include #include #def...
服务器端:
/*server.c*/
/* */
/* multithread tcp server */
/* uestc 2005/04/08 */
#include
#include
#include /*for bzero()*/
#include
#include
#include
#include /*for close()*/
#include
#include
#define PORT 1234
#define BUFSIZE 1024
#define BACKLOG 5
struct ARG {
int connfd;
struct sockaddr_in client;
};
void process_cli(int connectfd,struct sockaddr_in client);
void *start_routine(void * arg);
int main(){
struct sockaddr_in server;
struct sockaddr_in client;
int listenfd,connectfd;
pthread_t thread;
struct ARG *arg;
int sin_size;
pid_t pid;
if((listenfd=socket(AF_INET,SOCK_STREAM,0))==-1){
perror("socket() error");
exit(1);
}
int opt=SO_REUSEADDR;
setsockopt(listenfd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt));
bzero(&server,sizeof(server));
server.sin_family=AF_INET;
server.sin_port = htons(PORT);
server.sin_addr.s_addr= htonl(INADDR_ANY);
if(bind(listenfd,(struct sockaddr *)&server, sizeof(server))==-1){
perror("bind() errorn");
exit(1);
}
if(listen(listenfd, BACKLOG)==-1){
perror("listen error");
exit(1);
}
sin_size=sizeof(struct sockaddr_in);
while(1){
if((connectfd=accept(listenfd, (struct sockaddr *)&client,&sin_size))==-1){
perror("accept() error");
exit(1);
}
/*create thread*/
arg= (struct ARG*)malloc(sizeof(struct ARG));
arg->connfd = connectfd;
memcpy((void *)&(arg->client), &client, sizeof(client));
/*if (pthread_create(&thread, NULL, start_routine, (void *)arg)){
perror("Pthread_create() error");
exit(1);
}*/
printf("you get a connection from %s:%sn", inet_ntoa(arg->client.sin_addr),ntohs(arg->client.sin_port));
continue;
}
close(listenfd);
return 0;
}
void process_cli(int connectfd, struct sockaddr_in client){
int num;
int i=0;
char recvbuf[BUFSIZE],sendbuf[BUFSIZE];
/*display client ip port*/
printf("get a connection form %s %sn", inet_ntoa(client.sin_addr),ntohs(client.sin_port));
while(num=recv(connectfd, recvbuf, BUFSIZE,0)){
recvbuf[num]='';
if(recvbuf[0]==10){
printf("recieved an enter!n ");
continue;
}
printf("Received message from %s : %s",inet_ntoa(client.sin_addr),recvbuf);
for(i=0;i
/*server.c*/
/* */
/* multithread tcp server */
/* uestc 2005/04/08 */
#include
#include
#include /*for bzero()*/
#include
#include
#include
#include /*for close()*/
#include
#include
#define PORT 1234
#define BUFSIZE 1024
#define BACKLOG 5
struct ARG {
int connfd;
struct sockaddr_in client;
};
void process_cli(int connectfd,struct sockaddr_in client);
void *start_routine(void * arg);
int main(){
struct sockaddr_in server;
struct sockaddr_in client;
int listenfd,connectfd;
pthread_t thread;
struct ARG *arg;
int sin_size;
pid_t pid;
if((listenfd=socket(AF_INET,SOCK_STREAM,0))==-1){
perror("socket() error");
exit(1);
}
int opt=SO_REUSEADDR;
setsockopt(listenfd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt));
bzero(&server,sizeof(server));
server.sin_family=AF_INET;
server.sin_port = htons(PORT);
server.sin_addr.s_addr= htonl(INADDR_ANY);
if(bind(listenfd,(struct sockaddr *)&server, sizeof(server))==-1){
perror("bind() errorn");
exit(1);
}
if(listen(listenfd, BACKLOG)==-1){
perror("listen error");
exit(1);
}
sin_size=sizeof(struct sockaddr_in);
while(1){
if((connectfd=accept(listenfd, (struct sockaddr *)&client,&sin_size))==-1){
perror("accept() error");
exit(1);
}
/*create thread*/
arg= (struct ARG*)malloc(sizeof(struct ARG));
arg->connfd = connectfd;
memcpy((void *)&(arg->client), &client, sizeof(client));
/*if (pthread_create(&thread, NULL, start_routine, (void *)arg)){
perror("Pthread_create() error");
exit(1);
}*/
printf("you get a connection from %s:%sn", inet_ntoa(arg->client.sin_addr),ntohs(arg->client.sin_port));
continue;
}
close(listenfd);
return 0;
}
void process_cli(int connectfd, struct sockaddr_in client){
int num;
int i=0;
char recvbuf[BUFSIZE],sendbuf[BUFSIZE];
/*display client ip port*/
printf("get a connection form %s %sn", inet_ntoa(client.sin_addr),ntohs(client.sin_port));
while(num=recv(connectfd, recvbuf, BUFSIZE,0)){
recvbuf[num]='';
if(recvbuf[0]==10){
printf("recieved an enter!n ");
continue;
}
printf("Received message from %s : %s",inet_ntoa(client.sin_addr),recvbuf);
for(i=0;i