客户端、服务器源码

来源:互联网 发布:c语言打印图形 编辑:程序博客网 时间:2024/04/29 21:34

mpthserv.c

#include <pthread.h>

#include <stdio.h>

#include <sys/time.h>

#include <string.h>

#include <stdlib.h>

#include <sys/types.h>

#include <sys/socket.h>

#include <netinet/in.h>

#include <arpa/inet.h>


#define MAX 10


#define SERVER_PORT 5555


pthread_t thread[2];

pthread_mutex_t mut;
int number=0, i;

char clntName[INET_ADDRSTRLEN];


int serverSocket;

struct sockaddr_in server_addr;

struct sockaddr_in clientAddr;

int addr_len=sizeof(clientAddr);

int client;
char buffer[1024];

char sendbuf[1024];

int iDataNum;


void *thread1()
{

 for(;;)

        {
iDataNum=recv(client,buffer,1024,0);

if(iDataNum<0){

printf("recv() failed\n");

continue;

}

printf("client:> %s\n",buffer);

}
}


void *thread2()
{

 for(;;){

scanf("%s",sendbuf);


if(strcmp(sendbuf,"quit")==0)

break;


send(client,sendbuf,200,0);

        }


        pthread_exit(NULL);

}


void thread_create(void)
{

        int temp;

        memset(&thread, 0, sizeof(thread));
 
        if((temp = pthread_create(&thread[0], NULL, thread1, NULL)) != 0)       //comment2

 printf("线程1创建失败!\n");


        if((temp = pthread_create(&thread[1], NULL, thread2, NULL)) != 0)  //comment3

 printf("线程2创建失败");

}


void thread_wait(void)
{

        if(thread[0] !=0) {                   //comment4

pthread_join(thread[0],NULL);

        }

        if(thread[1] !=0) {                //comment5

pthread_join(thread[1],NULL);
}

}


int main()
{

if((serverSocket=socket(AF_INET,SOCK_STREAM,0))<0){

printf("socket() failed\n");

exit(1);

}
 

bzero(&server_addr,sizeof(server_addr));

server_addr.sin_family=AF_INET;
server_addr.sin_addr.s_addr=htonl(INADDR_ANY);

server_addr.sin_port=htons(SERVER_PORT);

if(bind(serverSocket,(struct sockaddr *)&server_addr,sizeof(server_addr))<0){

printf("bind() failed\n");

exit(1);

}


if(listen(serverSocket,5)<0){

printf("listen() failed\n");

}

while(1){

client=accept(serverSocket,(struct sockaddr *)&clientAddr,(socklen_t *)&addr_len);

if(client<0){
printf("accept() failed\n");
continue;

}

 printf("client:1接收成功\n");

 if(inet_ntop(AF_INET,&clientAddr.sin_addr.s_addr,clntName,sizeof(clntName))!=NULL){

 printf("Handling client %s socket %d\n",clntName,client);

 }
// IPSearch()

pthread_mutex_init(&mut,NULL);

        thread_create();

}

 return 0;

}
mpthcli.c

#include <pthread.h>
#include <stdio.h>
#include <sys/time.h>
#include <string.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>

#define MAX 10

#define SERVER_PORT 5555

pthread_t thread[2];
pthread_mutex_t mut;
int number=0, i;

struct sockaddr_in serverAddr;
int clientSocket,iDataNum;
char sendbuf[200];
char recvbuf[200];

void *thread1()
{
 for(;;)
        {
scanf("%s",sendbuf);

if(strcmp(sendbuf,"quit")==0)
break;
send(clientSocket,sendbuf,200,0);
        }
}

void *thread2()
{
 for(;;)
        {
iDataNum=recv(clientSocket,recvbuf,1024,0);
if(iDataNum<0){
printf("recv() failed\n");
continue;
}
printf("Server:>%s\n",recvbuf);
        }
}

void thread_create(void)
{
        int temp;
        memset(&thread, 0, sizeof(thread));          //comment1
       
        if((temp = pthread_create(&thread[0], NULL, thread1, NULL)) != 0)       //comment2
                printf("绾跨▼1鍒涘缓澶辫触!\n");

        if((temp = pthread_create(&thread[1], NULL, thread2, NULL)) != 0)  //comment3
                printf("绾跨▼2鍒涘缓澶辫触");
}

void thread_wait(void)
{
       
        if(thread[0] !=0) {                   //comment4
                pthread_join(thread[0],NULL);
        }
        if(thread[1] !=0) {                //comment5
                pthread_join(thread[1],NULL);
        }
}

int main()
{
if((clientSocket=socket(AF_INET,SOCK_STREAM,0))<0){
printf("socket() failed\n");
exit(1);
}
serverAddr.sin_family=AF_INET;
serverAddr.sin_addr.s_addr=inet_addr("127.0.0.1");
serverAddr.sin_port=htons(5555);

if(connect(clientSocket,(struct sockaddr *)&serverAddr,sizeof(serverAddr))<0){
printf("connect() failed\n");
exit(1);
}
        pthread_mutex_init(&mut,NULL);
        thread_create();
        thread_wait();
close(clientSocket);
  return 0;
}

原创粉丝点击