1。代理服务器在连接上了WEB服务器后,接收WEB服务器发送的小网页(比接受设置的buffer小的网页)没有问题,可在接受大的网页时收到的乱码。
2。数据收发中用的是结构体,实际传输的数据用的是BYTE,但传输图片信息始终无法显示。
以上2问题请大虾们帮帮忙。谢谢了
5 个解决方案
#1
GZ
#2
最好你能贴出一点代码来看看!
#3
谢谢各位大虾了。。。
通信结构体:
typedef struct Message
{
//the message type
char Status[11];
//the des ip
char DesIp[20];
//the des port
int DesPort;
//the message spi
char SPI[11];
//the message
byte Buffer[1024];
}Message;
通信结构体:
typedef struct Message
{
//the message type
char Status[11];
//the des ip
char DesIp[20];
//the des port
int DesPort;
//the message spi
char SPI[11];
//the message
byte Buffer[1024];
}Message;
#4
主要代码如下:
//connect to the internal net
int err;
SOCKET sd;
struct sockaddr_in sa;
WSADATA wsaData;
if(::WSAStartup(0x202,&wsaData)!=0)
{
WSACleanup();
return 0;
};
sd = socket(AF_INET, SOCK_STREAM, 0);
if (sd < 0)
{
printf("create socket error...\n");
return 0;
}
memset (&sa, '\0', sizeof(sa));
sa.sin_family = AF_INET;
//Server IP
sa.sin_addr.s_addr = inet_addr (ip);
//Server Port number
sa.sin_port = htons (port);
err = connect(sd, (struct sockaddr*) &sa,sizeof(sa));
if(err==SOCKET_ERROR)
{
printf("[%s:%d]connect to server error...\n",ip,port);
//constrcut message
FillMessage(&msg,"NEW",thr_Handler->ip, thr_Handler->port, spi);
strcpy((char *)msg.Buffer, "FALSE");
//AddHttp(buffer);
SSL_write(thr_Handler->ssl, (char *)&msg, sizeof(Message));
CloseConn(thr_Handler);
return 0;
}
printf("[%s:%d]connect to server successful...\n",ip,port);
//set the time out
int TimeOut=600; //设置发送超时60M秒
::setsockopt(sd,SOL_SOCKET,SO_SNDTIMEO,(char *)&TimeOut,sizeof(TimeOut));
TimeOut=600;//设置接收超时60M秒
::setsockopt(sd,SOL_SOCKET,SO_RCVTIMEO,(char *)&TimeOut,sizeof(TimeOut));
//constrcut message
FillMessage(&msg,"NEW",thr_Handler->ip, thr_Handler->port, spi);
strcpy((char *)msg.Buffer, "TRUE");
printf("[%s:%d]the send to the client successful info:%s\n", ip, port, msg.Buffer);
//AddHttp(buffer);
SSL_write(thr_Handler->ssl, (char *)&msg, sizeof(Message));
//check whether server send message
for(;;)
{
Message *mess = (Message *)malloc(sizeof(Message));
err = recv(sd, (char *)mess->Buffer, sizeof(mess->Buffer)-1, 0);
mess->Buffer[err] = '\0';
if(err>0)
{
printf("read %d chars\n",err);
printf("[%s:%d]first read from server the message is:%s\n", ip, port, mess->Buffer);
//sock is not closed and some message is readed
//constrcut message
FillMessage(mess,"COMMON",thr_Handler->ip, thr_Handler->port, spi);
//AddHttp(buffer);
SSL_write(thr_Handler->ssl, (char *)mess, sizeof(Message));
}
else if(err==0)
{
//sock is closed
//in common,it must be not occur
FillMessage(&msg,"CLOSE",thr_Handler->ip, thr_Handler->port, spi);
//AddHttp(buffer);
SSL_write(thr_Handler->ssl, (char *)mess, sizeof(Message));
}
else if(err==-1)
{
//sock is not closed and message is not
//constrcut message
FillMessage(mess,"COMPLETE",thr_Handler->ip, thr_Handler->port, spi);
strcpy((char *)mess->Buffer, "TRUE");
//AddHttp(buffer);
SSL_write(thr_Handler->ssl, (char *)mess, sizeof(Message));
break;
}
free(mess);
}//end for: new connection operation
//connect to the internal net
int err;
SOCKET sd;
struct sockaddr_in sa;
WSADATA wsaData;
if(::WSAStartup(0x202,&wsaData)!=0)
{
WSACleanup();
return 0;
};
sd = socket(AF_INET, SOCK_STREAM, 0);
if (sd < 0)
{
printf("create socket error...\n");
return 0;
}
memset (&sa, '\0', sizeof(sa));
sa.sin_family = AF_INET;
//Server IP
sa.sin_addr.s_addr = inet_addr (ip);
//Server Port number
sa.sin_port = htons (port);
err = connect(sd, (struct sockaddr*) &sa,sizeof(sa));
if(err==SOCKET_ERROR)
{
printf("[%s:%d]connect to server error...\n",ip,port);
//constrcut message
FillMessage(&msg,"NEW",thr_Handler->ip, thr_Handler->port, spi);
strcpy((char *)msg.Buffer, "FALSE");
//AddHttp(buffer);
SSL_write(thr_Handler->ssl, (char *)&msg, sizeof(Message));
CloseConn(thr_Handler);
return 0;
}
printf("[%s:%d]connect to server successful...\n",ip,port);
//set the time out
int TimeOut=600; //设置发送超时60M秒
::setsockopt(sd,SOL_SOCKET,SO_SNDTIMEO,(char *)&TimeOut,sizeof(TimeOut));
TimeOut=600;//设置接收超时60M秒
::setsockopt(sd,SOL_SOCKET,SO_RCVTIMEO,(char *)&TimeOut,sizeof(TimeOut));
//constrcut message
FillMessage(&msg,"NEW",thr_Handler->ip, thr_Handler->port, spi);
strcpy((char *)msg.Buffer, "TRUE");
printf("[%s:%d]the send to the client successful info:%s\n", ip, port, msg.Buffer);
//AddHttp(buffer);
SSL_write(thr_Handler->ssl, (char *)&msg, sizeof(Message));
//check whether server send message
for(;;)
{
Message *mess = (Message *)malloc(sizeof(Message));
err = recv(sd, (char *)mess->Buffer, sizeof(mess->Buffer)-1, 0);
mess->Buffer[err] = '\0';
if(err>0)
{
printf("read %d chars\n",err);
printf("[%s:%d]first read from server the message is:%s\n", ip, port, mess->Buffer);
//sock is not closed and some message is readed
//constrcut message
FillMessage(mess,"COMMON",thr_Handler->ip, thr_Handler->port, spi);
//AddHttp(buffer);
SSL_write(thr_Handler->ssl, (char *)mess, sizeof(Message));
}
else if(err==0)
{
//sock is closed
//in common,it must be not occur
FillMessage(&msg,"CLOSE",thr_Handler->ip, thr_Handler->port, spi);
//AddHttp(buffer);
SSL_write(thr_Handler->ssl, (char *)mess, sizeof(Message));
}
else if(err==-1)
{
//sock is not closed and message is not
//constrcut message
FillMessage(mess,"COMPLETE",thr_Handler->ip, thr_Handler->port, spi);
strcpy((char *)mess->Buffer, "TRUE");
//AddHttp(buffer);
SSL_write(thr_Handler->ssl, (char *)mess, sizeof(Message));
break;
}
free(mess);
}//end for: new connection operation
#5
//successful,session
for(;;)
{
int err = 0,nread = 0;
//read the message from the ssl client then check it
for(;;)
{
// Message msg;
// err = SSL_read(thr_Handler->ssl, (char *)&msg, sizeof(Message));
err = SSL_read(thr_Handler->ssl, buffer, sizeof(buffer)-1);
buffer[err] = '\0';
pMsg = (Message *)&buffer;
//check whether socket close
if(err==SOCKET_ERROR)
{
//set the close flag
memcpy(status,CLOSE,strlen(CLOSE));
status[strlen(CLOSE)] = '\0';
break;
}
//CString tmpData = DelHttp(buffer);
//check message
printf("[%s:%d]read %d chars,the status=%s\n",ip,port,err,pMsg->Status);
printf("[%s:%d]read message from client : %s\n",ip,port,pMsg->Buffer);
if(strcmp(pMsg->Status, "COMMON")==0)
{
//send the message to the internal server
err = send(sd, (char *)pMsg->Buffer, sizeof(pMsg->Buffer), 0);
printf("send %d chars\n",err);
if(err==SOCKET_ERROR)
{
//close the connnection of internal net
//constrcut message
Message msg;
FillMessage(&msg,"CLOSE",thr_Handler->ip, thr_Handler->port, spi);
strcpy((char *)pMsg->Buffer, "TRUE");
//AddHttp(buffer);
//send the close message to the ssl server
SSL_write(thr_Handler->ssl, (char *)&msg, sizeof(Message));
memcpy(status,CLOSE,strlen(CLOSE));
status[strlen(CLOSE)] = '\0';
//delete the connnect
closesocket(sd);
CloseConn(thr_Handler);
return 0;
}
}
else if(strcmp(pMsg->Status, "COMPELTE"))
{
break;
}
else if(strcmp(pMsg->Status, "CLOSE"))
{
break;
}
}//end for: ssl_client--->ssl_server--->internal_server
Sleep(2000);
//get the return message from the internal server
for(;;)
{
//check whether the socket close
//set the timeout,
//if the read return values == 0 ,the connection is closed
//if the read return values == -1 ,the connection is activity
//if the read return values >0 ,the read data ok
Message *mess = (Message *)malloc(sizeof(Message));
err = recv(sd, (char *)mess->Buffer, sizeof(mess->Buffer) ,0);
//mess->Buffer[err] = '\0';
if(err<sizeof(mess->Buffer))
{
mess->Buffer[err] = '\0';
}
if(err>0)
{
printf("[%s:%d]read %d chars\n",ip,port, strlen((char *)mess->Buffer));
printf("[%s:%d]the read message from the server : %s\n",ip,port, mess->Buffer);
//socket is not close,and read ok;
//constrcut message
FillMessage(mess, "COMMON",thr_Handler->ip, thr_Handler->port, spi);
//AddHttp(buffer);
SSL_write(thr_Handler->ssl, (char *)mess, sizeof(Message));
}
else if(err==0)
{
//socket is closed
printf("[%s:%d]the internal socket is closed.\n ",ip,port);
//constrcut message
FillMessage(mess, "CLOSE",thr_Handler->ip, thr_Handler->port, spi);
strcpy((char *)mess->Buffer, "TRUE");
//AddHttp(buffer);
SSL_write(thr_Handler->ssl, (char *)mess, sizeof(Message));
//set the exit flag
memcpy(status,CLOSE,strlen(CLOSE));
status[strlen(CLOSE)] = '\0';
break;
}
else if(err==-1)
{
//socket is not closed,and read false;
printf("[%s:%d]the internal socket is not closed\n",ip,port);
//constrcut message
FillMessage(mess, "COMPLETE",thr_Handler->ip, thr_Handler->port, spi);
strcpy((char *)mess->Buffer, "TRUE");
//AddHttp(buffer);
SSL_write(thr_Handler->ssl, (char *)mess, sizeof(Message));
break;
}
free(mess);
}//end for: internal server--->ssl_server--->ssl_client
//check whether error exit
if(strcmp(clientspi,(char *)pMsg->SPI)!=0)
{
printf("spi error...\n");
break;
}
if(strcmp(status,"CLOSE")==0)
{
printf("get the close message...\n");
break;
}
}//end for: the session
closesocket(sd);
for(;;)
{
int err = 0,nread = 0;
//read the message from the ssl client then check it
for(;;)
{
// Message msg;
// err = SSL_read(thr_Handler->ssl, (char *)&msg, sizeof(Message));
err = SSL_read(thr_Handler->ssl, buffer, sizeof(buffer)-1);
buffer[err] = '\0';
pMsg = (Message *)&buffer;
//check whether socket close
if(err==SOCKET_ERROR)
{
//set the close flag
memcpy(status,CLOSE,strlen(CLOSE));
status[strlen(CLOSE)] = '\0';
break;
}
//CString tmpData = DelHttp(buffer);
//check message
printf("[%s:%d]read %d chars,the status=%s\n",ip,port,err,pMsg->Status);
printf("[%s:%d]read message from client : %s\n",ip,port,pMsg->Buffer);
if(strcmp(pMsg->Status, "COMMON")==0)
{
//send the message to the internal server
err = send(sd, (char *)pMsg->Buffer, sizeof(pMsg->Buffer), 0);
printf("send %d chars\n",err);
if(err==SOCKET_ERROR)
{
//close the connnection of internal net
//constrcut message
Message msg;
FillMessage(&msg,"CLOSE",thr_Handler->ip, thr_Handler->port, spi);
strcpy((char *)pMsg->Buffer, "TRUE");
//AddHttp(buffer);
//send the close message to the ssl server
SSL_write(thr_Handler->ssl, (char *)&msg, sizeof(Message));
memcpy(status,CLOSE,strlen(CLOSE));
status[strlen(CLOSE)] = '\0';
//delete the connnect
closesocket(sd);
CloseConn(thr_Handler);
return 0;
}
}
else if(strcmp(pMsg->Status, "COMPELTE"))
{
break;
}
else if(strcmp(pMsg->Status, "CLOSE"))
{
break;
}
}//end for: ssl_client--->ssl_server--->internal_server
Sleep(2000);
//get the return message from the internal server
for(;;)
{
//check whether the socket close
//set the timeout,
//if the read return values == 0 ,the connection is closed
//if the read return values == -1 ,the connection is activity
//if the read return values >0 ,the read data ok
Message *mess = (Message *)malloc(sizeof(Message));
err = recv(sd, (char *)mess->Buffer, sizeof(mess->Buffer) ,0);
//mess->Buffer[err] = '\0';
if(err<sizeof(mess->Buffer))
{
mess->Buffer[err] = '\0';
}
if(err>0)
{
printf("[%s:%d]read %d chars\n",ip,port, strlen((char *)mess->Buffer));
printf("[%s:%d]the read message from the server : %s\n",ip,port, mess->Buffer);
//socket is not close,and read ok;
//constrcut message
FillMessage(mess, "COMMON",thr_Handler->ip, thr_Handler->port, spi);
//AddHttp(buffer);
SSL_write(thr_Handler->ssl, (char *)mess, sizeof(Message));
}
else if(err==0)
{
//socket is closed
printf("[%s:%d]the internal socket is closed.\n ",ip,port);
//constrcut message
FillMessage(mess, "CLOSE",thr_Handler->ip, thr_Handler->port, spi);
strcpy((char *)mess->Buffer, "TRUE");
//AddHttp(buffer);
SSL_write(thr_Handler->ssl, (char *)mess, sizeof(Message));
//set the exit flag
memcpy(status,CLOSE,strlen(CLOSE));
status[strlen(CLOSE)] = '\0';
break;
}
else if(err==-1)
{
//socket is not closed,and read false;
printf("[%s:%d]the internal socket is not closed\n",ip,port);
//constrcut message
FillMessage(mess, "COMPLETE",thr_Handler->ip, thr_Handler->port, spi);
strcpy((char *)mess->Buffer, "TRUE");
//AddHttp(buffer);
SSL_write(thr_Handler->ssl, (char *)mess, sizeof(Message));
break;
}
free(mess);
}//end for: internal server--->ssl_server--->ssl_client
//check whether error exit
if(strcmp(clientspi,(char *)pMsg->SPI)!=0)
{
printf("spi error...\n");
break;
}
if(strcmp(status,"CLOSE")==0)
{
printf("get the close message...\n");
break;
}
}//end for: the session
closesocket(sd);
#1
GZ
#2
最好你能贴出一点代码来看看!
#3
谢谢各位大虾了。。。
通信结构体:
typedef struct Message
{
//the message type
char Status[11];
//the des ip
char DesIp[20];
//the des port
int DesPort;
//the message spi
char SPI[11];
//the message
byte Buffer[1024];
}Message;
通信结构体:
typedef struct Message
{
//the message type
char Status[11];
//the des ip
char DesIp[20];
//the des port
int DesPort;
//the message spi
char SPI[11];
//the message
byte Buffer[1024];
}Message;
#4
主要代码如下:
//connect to the internal net
int err;
SOCKET sd;
struct sockaddr_in sa;
WSADATA wsaData;
if(::WSAStartup(0x202,&wsaData)!=0)
{
WSACleanup();
return 0;
};
sd = socket(AF_INET, SOCK_STREAM, 0);
if (sd < 0)
{
printf("create socket error...\n");
return 0;
}
memset (&sa, '\0', sizeof(sa));
sa.sin_family = AF_INET;
//Server IP
sa.sin_addr.s_addr = inet_addr (ip);
//Server Port number
sa.sin_port = htons (port);
err = connect(sd, (struct sockaddr*) &sa,sizeof(sa));
if(err==SOCKET_ERROR)
{
printf("[%s:%d]connect to server error...\n",ip,port);
//constrcut message
FillMessage(&msg,"NEW",thr_Handler->ip, thr_Handler->port, spi);
strcpy((char *)msg.Buffer, "FALSE");
//AddHttp(buffer);
SSL_write(thr_Handler->ssl, (char *)&msg, sizeof(Message));
CloseConn(thr_Handler);
return 0;
}
printf("[%s:%d]connect to server successful...\n",ip,port);
//set the time out
int TimeOut=600; //设置发送超时60M秒
::setsockopt(sd,SOL_SOCKET,SO_SNDTIMEO,(char *)&TimeOut,sizeof(TimeOut));
TimeOut=600;//设置接收超时60M秒
::setsockopt(sd,SOL_SOCKET,SO_RCVTIMEO,(char *)&TimeOut,sizeof(TimeOut));
//constrcut message
FillMessage(&msg,"NEW",thr_Handler->ip, thr_Handler->port, spi);
strcpy((char *)msg.Buffer, "TRUE");
printf("[%s:%d]the send to the client successful info:%s\n", ip, port, msg.Buffer);
//AddHttp(buffer);
SSL_write(thr_Handler->ssl, (char *)&msg, sizeof(Message));
//check whether server send message
for(;;)
{
Message *mess = (Message *)malloc(sizeof(Message));
err = recv(sd, (char *)mess->Buffer, sizeof(mess->Buffer)-1, 0);
mess->Buffer[err] = '\0';
if(err>0)
{
printf("read %d chars\n",err);
printf("[%s:%d]first read from server the message is:%s\n", ip, port, mess->Buffer);
//sock is not closed and some message is readed
//constrcut message
FillMessage(mess,"COMMON",thr_Handler->ip, thr_Handler->port, spi);
//AddHttp(buffer);
SSL_write(thr_Handler->ssl, (char *)mess, sizeof(Message));
}
else if(err==0)
{
//sock is closed
//in common,it must be not occur
FillMessage(&msg,"CLOSE",thr_Handler->ip, thr_Handler->port, spi);
//AddHttp(buffer);
SSL_write(thr_Handler->ssl, (char *)mess, sizeof(Message));
}
else if(err==-1)
{
//sock is not closed and message is not
//constrcut message
FillMessage(mess,"COMPLETE",thr_Handler->ip, thr_Handler->port, spi);
strcpy((char *)mess->Buffer, "TRUE");
//AddHttp(buffer);
SSL_write(thr_Handler->ssl, (char *)mess, sizeof(Message));
break;
}
free(mess);
}//end for: new connection operation
//connect to the internal net
int err;
SOCKET sd;
struct sockaddr_in sa;
WSADATA wsaData;
if(::WSAStartup(0x202,&wsaData)!=0)
{
WSACleanup();
return 0;
};
sd = socket(AF_INET, SOCK_STREAM, 0);
if (sd < 0)
{
printf("create socket error...\n");
return 0;
}
memset (&sa, '\0', sizeof(sa));
sa.sin_family = AF_INET;
//Server IP
sa.sin_addr.s_addr = inet_addr (ip);
//Server Port number
sa.sin_port = htons (port);
err = connect(sd, (struct sockaddr*) &sa,sizeof(sa));
if(err==SOCKET_ERROR)
{
printf("[%s:%d]connect to server error...\n",ip,port);
//constrcut message
FillMessage(&msg,"NEW",thr_Handler->ip, thr_Handler->port, spi);
strcpy((char *)msg.Buffer, "FALSE");
//AddHttp(buffer);
SSL_write(thr_Handler->ssl, (char *)&msg, sizeof(Message));
CloseConn(thr_Handler);
return 0;
}
printf("[%s:%d]connect to server successful...\n",ip,port);
//set the time out
int TimeOut=600; //设置发送超时60M秒
::setsockopt(sd,SOL_SOCKET,SO_SNDTIMEO,(char *)&TimeOut,sizeof(TimeOut));
TimeOut=600;//设置接收超时60M秒
::setsockopt(sd,SOL_SOCKET,SO_RCVTIMEO,(char *)&TimeOut,sizeof(TimeOut));
//constrcut message
FillMessage(&msg,"NEW",thr_Handler->ip, thr_Handler->port, spi);
strcpy((char *)msg.Buffer, "TRUE");
printf("[%s:%d]the send to the client successful info:%s\n", ip, port, msg.Buffer);
//AddHttp(buffer);
SSL_write(thr_Handler->ssl, (char *)&msg, sizeof(Message));
//check whether server send message
for(;;)
{
Message *mess = (Message *)malloc(sizeof(Message));
err = recv(sd, (char *)mess->Buffer, sizeof(mess->Buffer)-1, 0);
mess->Buffer[err] = '\0';
if(err>0)
{
printf("read %d chars\n",err);
printf("[%s:%d]first read from server the message is:%s\n", ip, port, mess->Buffer);
//sock is not closed and some message is readed
//constrcut message
FillMessage(mess,"COMMON",thr_Handler->ip, thr_Handler->port, spi);
//AddHttp(buffer);
SSL_write(thr_Handler->ssl, (char *)mess, sizeof(Message));
}
else if(err==0)
{
//sock is closed
//in common,it must be not occur
FillMessage(&msg,"CLOSE",thr_Handler->ip, thr_Handler->port, spi);
//AddHttp(buffer);
SSL_write(thr_Handler->ssl, (char *)mess, sizeof(Message));
}
else if(err==-1)
{
//sock is not closed and message is not
//constrcut message
FillMessage(mess,"COMPLETE",thr_Handler->ip, thr_Handler->port, spi);
strcpy((char *)mess->Buffer, "TRUE");
//AddHttp(buffer);
SSL_write(thr_Handler->ssl, (char *)mess, sizeof(Message));
break;
}
free(mess);
}//end for: new connection operation
#5
//successful,session
for(;;)
{
int err = 0,nread = 0;
//read the message from the ssl client then check it
for(;;)
{
// Message msg;
// err = SSL_read(thr_Handler->ssl, (char *)&msg, sizeof(Message));
err = SSL_read(thr_Handler->ssl, buffer, sizeof(buffer)-1);
buffer[err] = '\0';
pMsg = (Message *)&buffer;
//check whether socket close
if(err==SOCKET_ERROR)
{
//set the close flag
memcpy(status,CLOSE,strlen(CLOSE));
status[strlen(CLOSE)] = '\0';
break;
}
//CString tmpData = DelHttp(buffer);
//check message
printf("[%s:%d]read %d chars,the status=%s\n",ip,port,err,pMsg->Status);
printf("[%s:%d]read message from client : %s\n",ip,port,pMsg->Buffer);
if(strcmp(pMsg->Status, "COMMON")==0)
{
//send the message to the internal server
err = send(sd, (char *)pMsg->Buffer, sizeof(pMsg->Buffer), 0);
printf("send %d chars\n",err);
if(err==SOCKET_ERROR)
{
//close the connnection of internal net
//constrcut message
Message msg;
FillMessage(&msg,"CLOSE",thr_Handler->ip, thr_Handler->port, spi);
strcpy((char *)pMsg->Buffer, "TRUE");
//AddHttp(buffer);
//send the close message to the ssl server
SSL_write(thr_Handler->ssl, (char *)&msg, sizeof(Message));
memcpy(status,CLOSE,strlen(CLOSE));
status[strlen(CLOSE)] = '\0';
//delete the connnect
closesocket(sd);
CloseConn(thr_Handler);
return 0;
}
}
else if(strcmp(pMsg->Status, "COMPELTE"))
{
break;
}
else if(strcmp(pMsg->Status, "CLOSE"))
{
break;
}
}//end for: ssl_client--->ssl_server--->internal_server
Sleep(2000);
//get the return message from the internal server
for(;;)
{
//check whether the socket close
//set the timeout,
//if the read return values == 0 ,the connection is closed
//if the read return values == -1 ,the connection is activity
//if the read return values >0 ,the read data ok
Message *mess = (Message *)malloc(sizeof(Message));
err = recv(sd, (char *)mess->Buffer, sizeof(mess->Buffer) ,0);
//mess->Buffer[err] = '\0';
if(err<sizeof(mess->Buffer))
{
mess->Buffer[err] = '\0';
}
if(err>0)
{
printf("[%s:%d]read %d chars\n",ip,port, strlen((char *)mess->Buffer));
printf("[%s:%d]the read message from the server : %s\n",ip,port, mess->Buffer);
//socket is not close,and read ok;
//constrcut message
FillMessage(mess, "COMMON",thr_Handler->ip, thr_Handler->port, spi);
//AddHttp(buffer);
SSL_write(thr_Handler->ssl, (char *)mess, sizeof(Message));
}
else if(err==0)
{
//socket is closed
printf("[%s:%d]the internal socket is closed.\n ",ip,port);
//constrcut message
FillMessage(mess, "CLOSE",thr_Handler->ip, thr_Handler->port, spi);
strcpy((char *)mess->Buffer, "TRUE");
//AddHttp(buffer);
SSL_write(thr_Handler->ssl, (char *)mess, sizeof(Message));
//set the exit flag
memcpy(status,CLOSE,strlen(CLOSE));
status[strlen(CLOSE)] = '\0';
break;
}
else if(err==-1)
{
//socket is not closed,and read false;
printf("[%s:%d]the internal socket is not closed\n",ip,port);
//constrcut message
FillMessage(mess, "COMPLETE",thr_Handler->ip, thr_Handler->port, spi);
strcpy((char *)mess->Buffer, "TRUE");
//AddHttp(buffer);
SSL_write(thr_Handler->ssl, (char *)mess, sizeof(Message));
break;
}
free(mess);
}//end for: internal server--->ssl_server--->ssl_client
//check whether error exit
if(strcmp(clientspi,(char *)pMsg->SPI)!=0)
{
printf("spi error...\n");
break;
}
if(strcmp(status,"CLOSE")==0)
{
printf("get the close message...\n");
break;
}
}//end for: the session
closesocket(sd);
for(;;)
{
int err = 0,nread = 0;
//read the message from the ssl client then check it
for(;;)
{
// Message msg;
// err = SSL_read(thr_Handler->ssl, (char *)&msg, sizeof(Message));
err = SSL_read(thr_Handler->ssl, buffer, sizeof(buffer)-1);
buffer[err] = '\0';
pMsg = (Message *)&buffer;
//check whether socket close
if(err==SOCKET_ERROR)
{
//set the close flag
memcpy(status,CLOSE,strlen(CLOSE));
status[strlen(CLOSE)] = '\0';
break;
}
//CString tmpData = DelHttp(buffer);
//check message
printf("[%s:%d]read %d chars,the status=%s\n",ip,port,err,pMsg->Status);
printf("[%s:%d]read message from client : %s\n",ip,port,pMsg->Buffer);
if(strcmp(pMsg->Status, "COMMON")==0)
{
//send the message to the internal server
err = send(sd, (char *)pMsg->Buffer, sizeof(pMsg->Buffer), 0);
printf("send %d chars\n",err);
if(err==SOCKET_ERROR)
{
//close the connnection of internal net
//constrcut message
Message msg;
FillMessage(&msg,"CLOSE",thr_Handler->ip, thr_Handler->port, spi);
strcpy((char *)pMsg->Buffer, "TRUE");
//AddHttp(buffer);
//send the close message to the ssl server
SSL_write(thr_Handler->ssl, (char *)&msg, sizeof(Message));
memcpy(status,CLOSE,strlen(CLOSE));
status[strlen(CLOSE)] = '\0';
//delete the connnect
closesocket(sd);
CloseConn(thr_Handler);
return 0;
}
}
else if(strcmp(pMsg->Status, "COMPELTE"))
{
break;
}
else if(strcmp(pMsg->Status, "CLOSE"))
{
break;
}
}//end for: ssl_client--->ssl_server--->internal_server
Sleep(2000);
//get the return message from the internal server
for(;;)
{
//check whether the socket close
//set the timeout,
//if the read return values == 0 ,the connection is closed
//if the read return values == -1 ,the connection is activity
//if the read return values >0 ,the read data ok
Message *mess = (Message *)malloc(sizeof(Message));
err = recv(sd, (char *)mess->Buffer, sizeof(mess->Buffer) ,0);
//mess->Buffer[err] = '\0';
if(err<sizeof(mess->Buffer))
{
mess->Buffer[err] = '\0';
}
if(err>0)
{
printf("[%s:%d]read %d chars\n",ip,port, strlen((char *)mess->Buffer));
printf("[%s:%d]the read message from the server : %s\n",ip,port, mess->Buffer);
//socket is not close,and read ok;
//constrcut message
FillMessage(mess, "COMMON",thr_Handler->ip, thr_Handler->port, spi);
//AddHttp(buffer);
SSL_write(thr_Handler->ssl, (char *)mess, sizeof(Message));
}
else if(err==0)
{
//socket is closed
printf("[%s:%d]the internal socket is closed.\n ",ip,port);
//constrcut message
FillMessage(mess, "CLOSE",thr_Handler->ip, thr_Handler->port, spi);
strcpy((char *)mess->Buffer, "TRUE");
//AddHttp(buffer);
SSL_write(thr_Handler->ssl, (char *)mess, sizeof(Message));
//set the exit flag
memcpy(status,CLOSE,strlen(CLOSE));
status[strlen(CLOSE)] = '\0';
break;
}
else if(err==-1)
{
//socket is not closed,and read false;
printf("[%s:%d]the internal socket is not closed\n",ip,port);
//constrcut message
FillMessage(mess, "COMPLETE",thr_Handler->ip, thr_Handler->port, spi);
strcpy((char *)mess->Buffer, "TRUE");
//AddHttp(buffer);
SSL_write(thr_Handler->ssl, (char *)mess, sizeof(Message));
break;
}
free(mess);
}//end for: internal server--->ssl_server--->ssl_client
//check whether error exit
if(strcmp(clientspi,(char *)pMsg->SPI)!=0)
{
printf("spi error...\n");
break;
}
if(strcmp(status,"CLOSE")==0)
{
printf("get the close message...\n");
break;
}
}//end for: the session
closesocket(sd);