#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <stdlib.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <pthread.h>
#include <memory.h>
pthread_t thread[2];
void *send_msg(void *ip_msg)
{
char send_buffer[1024];
struct sockaddr_in * friend_addr;
friend_addr=(struct sockaddr_in *)ip_msg;
struct sockaddr_in addr;
addr.sin_family=AF_INET;
addr.sin_addr.s_addr=(friend_addr->sin_addr.s_addr);
//addr.sin_addr.s_addr=inet_addr("127.0.0.1");
printf("ip is %d.%d.%d.%d\n",addr.sin_addr.s_addr>>24,addr.sin_addr.s_addr>>16&255,addr.sin_addr.s_addr>>8&255,addr.sin_addr.s_addr&255);
addr.sin_port=htons(12346);
int sockfd;
while(1)
{
memset(send_buffer,0,sizeof(send_buffer));
scanf("%s",send_buffer);
sockfd=socket(AF_INET,SOCK_STREAM,0);
if(sockfd<0)
{
perror("send socket error");
break;
}
if(connect(sockfd,(struct sockaddr *)&addr,sizeof(addr))<0)
{
perror("connect socket error");
break;
}
write(sockfd,send_buffer,strlen(send_buffer));
close(sockfd);
}
pthread_exit(NULL);
}
void *rec_msg()
{
char rec_buffer[1024];
struct sockaddr_in me_addr;
struct sockaddr_in friend_addr;
int me_sockfd,friend_sockfd;
me_addr.sin_family=AF_INET;
me_addr.sin_addr.s_addr=htonl(INADDR_ANY);
me_addr.sin_port=htons(12345);
me_sockfd=socket(AF_INET,SOCK_STREAM,0);
if(me_sockfd<0)
{
perror("socket error");
pthread_exit(NULL);
return;
}
bind(me_sockfd,(struct sockaddr *)&me_addr,sizeof(me_addr));
listen(me_sockfd,5);
int times=0;
while(1)
{
int len=sizeof(friend_addr);
memset(rec_buffer,0,sizeof(rec_buffer));
friend_sockfd=accept(me_sockfd,(struct sockaddr *)&friend_addr,&len);
if(times==0)
{
times++;
int temp=pthread_create(&thread[1],NULL,send_msg,&friend_addr);
if(temp<0)
{
break;
}
}
read(friend_sockfd,rec_buffer,sizeof(rec_buffer));
printf("friend says:%s\n",rec_buffer);
close(friend_sockfd);
}
pthread_exit(NULL);
}
void thread_create()
{
memset(&thread,0,sizeof(thread));
int temp;
temp=pthread_create(&thread[0],NULL,rec_msg,NULL);
if(temp<0)
{
perror("thread 1 fail\n");
}
}
void thread_wait()
{
if(thread[0]!=0)
{
pthread_join(thread[0],NULL);
}
if(thread[1]!=0)
{
pthread_join(thread[1],NULL);
}
}
int main()
{
thread_create();
thread_wait();
return 0;
}
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <stdlib.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <pthread.h>
#include <memory.h>
pthread_t thread[2];
void *send_msg(void *ip_msg)
{
char send_buffer[1024];
struct sockaddr_in * friend_addr;
friend_addr=(struct sockaddr_in *)ip_msg;
struct sockaddr_in addr;
addr.sin_family=AF_INET;
addr.sin_addr.s_addr=(friend_addr->sin_addr.s_addr);
//addr.sin_addr.s_addr=inet_addr("127.0.0.1");
printf("ip is %d.%d.%d.%d\n",addr.sin_addr.s_addr>>24,addr.sin_addr.s_addr>>16&255,addr.sin_addr.s_addr>>8&255,addr.sin_addr.s_addr&255);
addr.sin_port=htons(12346);
int sockfd;
while(1)
{
memset(send_buffer,0,sizeof(send_buffer));
scanf("%s",send_buffer);
sockfd=socket(AF_INET,SOCK_STREAM,0);
if(sockfd<0)
{
perror("send socket error");
break;
}
if(connect(sockfd,(struct sockaddr *)&addr,sizeof(addr))<0)
{
perror("connect socket error");
break;
}
write(sockfd,send_buffer,strlen(send_buffer));
close(sockfd);
}
pthread_exit(NULL);
}
void *rec_msg()
{
char rec_buffer[1024];
struct sockaddr_in me_addr;
struct sockaddr_in friend_addr;
int me_sockfd,friend_sockfd;
me_addr.sin_family=AF_INET;
me_addr.sin_addr.s_addr=htonl(INADDR_ANY);
me_addr.sin_port=htons(12345);
me_sockfd=socket(AF_INET,SOCK_STREAM,0);
if(me_sockfd<0)
{
perror("socket error");
pthread_exit(NULL);
return;
}
bind(me_sockfd,(struct sockaddr *)&me_addr,sizeof(me_addr));
listen(me_sockfd,5);
int times=0;
while(1)
{
int len=sizeof(friend_addr);
memset(rec_buffer,0,sizeof(rec_buffer));
friend_sockfd=accept(me_sockfd,(struct sockaddr *)&friend_addr,&len);
if(times==0)
{
times++;
int temp=pthread_create(&thread[1],NULL,send_msg,&friend_addr);
if(temp<0)
{
break;
}
}
read(friend_sockfd,rec_buffer,sizeof(rec_buffer));
printf("friend says:%s\n",rec_buffer);
close(friend_sockfd);
}
pthread_exit(NULL);
}
void thread_create()
{
memset(&thread,0,sizeof(thread));
int temp;
temp=pthread_create(&thread[0],NULL,rec_msg,NULL);
if(temp<0)
{
perror("thread 1 fail\n");
}
}
void thread_wait()
{
if(thread[0]!=0)
{
pthread_join(thread[0],NULL);
}
if(thread[1]!=0)
{
pthread_join(thread[1],NULL);
}
}
int main()
{
thread_create();
thread_wait();
return 0;
}