//server.c
#include<stdio.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<unistd.h>
#include<errno.h>
#include<string.h>
#include<stdlib.h>
#define SERV_PORT 3008
int sock_fd; //套接子描述符号
int recv_num;
int send_num;
int client_len;
char recv_buf[20];
char send_buf[20];
struct sockaddr_in addr_serv;
struct sockaddr_in addr_client;//服务器和客户端地址
void thread_send();
void thread_recv();
int main()
{
pthread_t ids1;
pthread_t ids2;
sock_fd = socket(AF_INET,SOCK_DGRAM,0);
if(sock_fd < 0)
{
perror("socket");
exit(1);
}
else
{
printf("sock sucessful\n");
}
//初始化服务器断地址
memset(&addr_serv,0,sizeof(struct sockaddr_in));
addr_serv.sin_family = AF_INET;//协议族
addr_serv.sin_port = htons(SERV_PORT);
addr_serv.sin_addr.s_addr = htonl(INADDR_ANY);//任意本地址
client_len = sizeof(struct sockaddr_in);
/*绑定套接子*/
if(bind(sock_fd,(struct sockaddr *)&addr_serv,sizeof(struct sockaddr_in))<0 )
{
perror("bind");
exit(1);
}
else
{
printf("bind sucess\n");
}
if(pthread_create(&ids1,NULL,(void *) &thread_send,NULL)!=0)
{
printf("线程创建错误\n");
exit(1);
}
if(pthread_create(&ids2,NULL,(void *) & thread_recv,NULL)!=0)
{
printf("线程创建错误\n");
exit(1);
}
while(1);
pthread_join(ids1,NULL);
pthread_join(ids2,NULL);
close(sock_fd);
return 0;
}
void thread_send()
{
while(1)
{
fgets(send_buf,sizeof(send_buf),stdin);
send_num = sendto(sock_fd,send_buf,sizeof(send_buf),0,(struct sockaddr *)&addr_client,client_len);
if(send_num < 0)
{
perror("sendto");
exit(1);
}
else
{
// printf("send sucessful\n");
}
}
}
void thread_recv()
{
while(1)
{
recv_num = recvfrom(sock_fd,recv_buf,sizeof(recv_buf),0,(struct sockaddr *)&addr_client,&client_len);
if(recv_num < 0)
{
printf("bad\n");
perror("again recvfrom");
exit(1);
}
else
{
recv_buf[recv_num]='\0';
printf("cleint said:%s",recv_buf);
}
}
}
- 1
- 2
前往页