请写一个 linux下 监听socket 发thread的c程序
1个回答
2013-09-07
展开全部
我亲自上机帮你写好了,服务器端用的是多线程,每个客户端与服务端连接后,服务端都会分配一个线程为它服务
客户端:
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <netdb.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <sys/socket.h>
#define SERVPORT 3333
#define MAXDATASIZE 100
#define SERVIP "192.168.1.230"
main(int argc,char *argv[]){
int sockfd,sendbytes;
char buf[MAXDATASIZE];
struct hostent *host;
struct sockaddr_in serv_addr;
if(argc < 2){
fprintf(stderr,"Please enter the server's hostname!\
");
exit(1);
}
if((host=gethostbyname(argv[1]))==NULL){
perror("gethostbyname");
exit(1);
}
if((sockfd=socket(AF_INET,SOCK_STREAM,0))==-1){
perror("socket");
exit(1);
}
serv_addr.sin_family=AF_INET;
serv_addr.sin_port=htons(SERVPORT);
serv_addr.sin_addr.s_addr = inet_addr(SERVIP);
bzero(&(serv_addr.sin_zero),8);
if(connect(sockfd,(struct sockaddr *)&serv_addr,\\
sizeof(struct sockaddr))==-1){
perror("connect");
exit(1);
}
if((sendbytes=send(sockfd,"hello",5,0))==-1){
perror("send");
exit(1);
}
close(sockfd);
}
服务端:
#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>
#include <pthread.h>
#define SERVPORT 3333
#define BACKLOG 10
#define MAX_CONNECTED_NO 10
#define MAXDATASIZE 5
#define SERVIP "192.168.1.230"
void mypthread(void *sockfd)
{
char buf[MAXDATASIZE];
int recvbytes;
pthread_detach(pthread_self());
if((recvbytes=recv((int)sockfd, buf, MAXDATASIZE, 0)) == -1){
perror("recv");
exit(1);
}
printf("received a connection :%s\
", buf);
close((int)sockfd);
}
int main()
{
struct sockaddr_in server_sockaddr,client_sockaddr;
int sin_size;
int sockfd,client_fd;
pthread_t tid;
if((sockfd = socket(AF_INET,SOCK_STREAM,0))==-1){
perror("socket");
exit(1);
}
printf("socket success!,sockfd=%d\
",sockfd);
server_sockaddr.sin_family=AF_INET;
server_sockaddr.sin_port=htons(SERVPORT);
server_sockaddr.sin_addr.s_addr = inet_addr(SERVIP);
bzero(&(server_sockaddr.sin_zero),8);
if(bind(sockfd,(struct sockaddr *)&server_sockaddr,sizeof(struct sockaddr))==-1){
perror("bind");
exit(1);
}
printf("bind success!\
");
if(listen(sockfd,BACKLOG)==-1){
perror("listen");
exit(1);
}
printf("listening....\
");
sin_size = sizeof(client_sockaddr);
if((client_fd=accept(sockfd,(struct sockaddr *)&client_sockaddr,&sin_size))==-1){
perror("accept");
exit(1);
}
pthread_create(&tid, NULL, &mypthread, (void *)client_fd);
return 0;
}
编译:
gcc client.c -o client
gcc server.c -lpthread -o server
运行:
开启两个终端
第一个:
./server
第二个:
./client 192.168.1.230
友情提醒:你的本机IP必须和程序里的IP一样,否则运行会出错
客户端:
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <netdb.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <sys/socket.h>
#define SERVPORT 3333
#define MAXDATASIZE 100
#define SERVIP "192.168.1.230"
main(int argc,char *argv[]){
int sockfd,sendbytes;
char buf[MAXDATASIZE];
struct hostent *host;
struct sockaddr_in serv_addr;
if(argc < 2){
fprintf(stderr,"Please enter the server's hostname!\
");
exit(1);
}
if((host=gethostbyname(argv[1]))==NULL){
perror("gethostbyname");
exit(1);
}
if((sockfd=socket(AF_INET,SOCK_STREAM,0))==-1){
perror("socket");
exit(1);
}
serv_addr.sin_family=AF_INET;
serv_addr.sin_port=htons(SERVPORT);
serv_addr.sin_addr.s_addr = inet_addr(SERVIP);
bzero(&(serv_addr.sin_zero),8);
if(connect(sockfd,(struct sockaddr *)&serv_addr,\\
sizeof(struct sockaddr))==-1){
perror("connect");
exit(1);
}
if((sendbytes=send(sockfd,"hello",5,0))==-1){
perror("send");
exit(1);
}
close(sockfd);
}
服务端:
#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>
#include <pthread.h>
#define SERVPORT 3333
#define BACKLOG 10
#define MAX_CONNECTED_NO 10
#define MAXDATASIZE 5
#define SERVIP "192.168.1.230"
void mypthread(void *sockfd)
{
char buf[MAXDATASIZE];
int recvbytes;
pthread_detach(pthread_self());
if((recvbytes=recv((int)sockfd, buf, MAXDATASIZE, 0)) == -1){
perror("recv");
exit(1);
}
printf("received a connection :%s\
", buf);
close((int)sockfd);
}
int main()
{
struct sockaddr_in server_sockaddr,client_sockaddr;
int sin_size;
int sockfd,client_fd;
pthread_t tid;
if((sockfd = socket(AF_INET,SOCK_STREAM,0))==-1){
perror("socket");
exit(1);
}
printf("socket success!,sockfd=%d\
",sockfd);
server_sockaddr.sin_family=AF_INET;
server_sockaddr.sin_port=htons(SERVPORT);
server_sockaddr.sin_addr.s_addr = inet_addr(SERVIP);
bzero(&(server_sockaddr.sin_zero),8);
if(bind(sockfd,(struct sockaddr *)&server_sockaddr,sizeof(struct sockaddr))==-1){
perror("bind");
exit(1);
}
printf("bind success!\
");
if(listen(sockfd,BACKLOG)==-1){
perror("listen");
exit(1);
}
printf("listening....\
");
sin_size = sizeof(client_sockaddr);
if((client_fd=accept(sockfd,(struct sockaddr *)&client_sockaddr,&sin_size))==-1){
perror("accept");
exit(1);
}
pthread_create(&tid, NULL, &mypthread, (void *)client_fd);
return 0;
}
编译:
gcc client.c -o client
gcc server.c -lpthread -o server
运行:
开启两个终端
第一个:
./server
第二个:
./client 192.168.1.230
友情提醒:你的本机IP必须和程序里的IP一样,否则运行会出错
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询