Socket服务端和客户端(C++,CodeBlocks+GCC编译)

时间:2021-10-22 00:21:33
//main.cpp
1 #include "j_socket.h"
#include <stdio.h>
#include <pthread.h>
static int port=;
j_server* ser;
void* main_listen( void* args)
{
ser=new j_server(port);
ser->j_listen();
}
int main()
{
static pthread_t listen_p;
int ret;
ret= pthread_create( &listen_p, NULL, main_listen, NULL ); //参数:创建的线程id,线程参数,线程运行函数的起始地址,运行函数的参数
if( ret != ) //创建线程成功返回0
{
printf("pthread_create error:error_code=%d\n",ret );
}
static j_client* cli;
cli =new j_client("127.0.0.1",port);
cli->j_send();
return ;
}
//j_socket.h
1 #ifndef J_SOCKET_H_INCLUDED
#define J_SOCKET_H_INCLUDED
#include <stdio.h>
#include <errno.h>
#include <winsock2.h>
#include <pthread.h> #define INIT_ERROR 1
#define BIND_ERROR 2
#define LISTEN_ERROR 3
struct rec_args{
int * l;
SOCKET* s;
};
void* receive( void* args)
{
char recvBuf[];
rec_args *a=(rec_args*)args;
while(true)
{
recv(*(a->s),recvBuf,,);
printf("receive message: %s\n",recvBuf);
printf("111111\n");
if(strcmp(recvBuf,"end")==){break;} }
closesocket(*(a->s));
(*(a->l))--;
printf("22222\n");
return NULL;
}
class j_server
{
private:
SOCKET Server_Sock;
SOCKADDR_IN server_addr;
SOCKADDR_IN client_addr; int max_num;
int link_num=;
int error_num; public:
j_server(int port)
{
WSADATA wsa;
/*初始化socket资源*/
if (WSAStartup(MAKEWORD(,),&wsa) != )
{
error_num=INIT_ERROR;
return;
} if((Server_Sock = socket(AF_INET, SOCK_STREAM, ))==-)
{
error_num=INIT_ERROR;
return;
}
ZeroMemory((char *)&server_addr,sizeof(server_addr));
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(port); /*本地监听端口*/
server_addr.sin_addr.s_addr = htonl(INADDR_ANY); /*有IP*/ if(bind(Server_Sock,(struct sockaddr *)&server_addr,sizeof(server_addr))==-)
{
error_num=BIND_ERROR;
return;
}
if(listen(Server_Sock,)==-) //其中第二个参数代表能够接收的最多的连接数
{
error_num=LISTEN_ERROR;
return;
}
max_num=; }
int j_listen()
{
int len=sizeof(SOCKADDR);
while(true)
{
if(link_num>max_num)
{
Sleep();
continue;
}
SOCKET Command_Sock = accept(Server_Sock, (SOCKADDR*)&client_addr,&len);
if(Command_Sock == INVALID_SOCKET)
{
printf("accept error !");
closesocket(Command_Sock);
}
link_num++;
pthread_t listen_p;
int ret;
rec_args a;
a.l=&link_num;
a.s=&Command_Sock;
ret= pthread_create( &listen_p, NULL, receive,(void *)&a ); //参数:创建的线程id,线程参数,线程运行函数的起始地址,运行函数的参数
}
return ;
} };
class j_client
{
private:
SOCKET Client_Sock;
SOCKADDR_IN server_addr;
char sendBuf[];
int error_num;
public: j_client(char * server_addr_str,int port)
{
WSADATA wsa;
/*初始化socket资源*/
if (WSAStartup(MAKEWORD(,),&wsa) != )
{
error_num=INIT_ERROR;
return; //代表失败
}
if((Client_Sock = socket(AF_INET, SOCK_STREAM, ))==-)
{
error_num=INIT_ERROR;
return; //代表失败
}
server_addr.sin_addr.S_un.S_addr=inet_addr(server_addr_str);
server_addr.sin_family=AF_INET;
server_addr.sin_port=htons(port);
if(connect(Client_Sock,(SOCKADDR*)&server_addr,sizeof(SOCKADDR)) ==-)
{
printf("connect error !");
//printf( "%d ", WSAGetLastError());
}
}
int j_send()
{
while(true)
{
printf("input message: ");
scanf("%s",sendBuf);
if(send(Client_Sock,sendBuf,strlen(sendBuf)+,)!=(strlen(sendBuf)+))
{
printf("send error !");
//printf( "%d ", WSAGetLastError());
}
}
return ;
}
int j_close()
{
closesocket(Client_Sock);
WSACleanup();
}
}; #endif // J_SOCKET_H_INCLUDED