void __fastcall TFormzMain::ClientSocketDataReceived()
{
TIdBytes SocketReceiveArray;
unsigned char ReceiveDataBuffer[1024];
try
{
IdTCPClient->IOHandler->CheckForDisconnect(true, true);
IdTCPClient->IOHandler->CheckForDataOnSource(100);
IdTCPClient->CheckForGracefulDisconnect(true);
SocketReceiveSize = IdTCPClient->IOHandler->InputBuffer->Size;
}
catch(...)
{
return;
}
if (SocketReceiveSize != 0)
{
try
{
IdTCPClient->IOHandler->ReadBytes(SocketReceiveArray, SocketReceiveSize, false);
BytesToRaw(SocketReceiveArray, &ReceiveDataBuffer, SocketReceiveSize);
}
catch(...)
{
return;
}
}
}
void __fastcall TForm1::IdTCPServer1Execute(TIdContext *AContext)
{
int RecieveSize;
unsigned char ReceiveDataBuffer[1024];
try
{
AContext->Connection->IOHandler->CheckForDisconnect(true, true);
AContext->Connection->CheckForGracefulDisconnect(true);
RecieveSize = AContext->Connection->IOHandler->InputBuffer->Size;
}
catch(...)
{
return;
}
if (RecieveSize >= 1)
{
try
{
AContext->Connection->IOHandler->ReadBytes(SocketReceiveArray, RecieveSize, false);
}
catch (EIdConnClosedGracefully &E)
{
AContext->Connection->IOHandler->InputBuffer->Clear();
AContext->Connection->Disconnect();
return;
}
BytesToRaw(SocketReceiveArray, ReceiveDataBuffer, RecieveSize);
}
}
chrisb 님이 쓰신 글 :
: Server와 Client 프로그램을 짜서 값을 주고 받는 프로그램을 짜고 있습니다.
:
: 값을 주고 받는 건 잘 되고 있습니다만..
:
: 문제가 조금 있습니다.
:
: 1. 서버가 갑자기 죽거나 정전 등의 경우가 발생했을 경우에 Client쪽에서 접속을 종료하게 하고 싶은 것이 한 가지이고,
:
: 2.현재 서버를 돌려놓고 클라이언트에서 IdClient->Connect()만 하면 Server 쪽에 값을 읽는 부분에서 멈춰서 다음으로 넘어가지도 않고 그 자리에서 대기만 하고 있는 것 같습니다.
:
: 이로 인해서 발생하는 문제는 프로그램이 그냥 꺼지지 않고 에러를 보내거나 작업관리자에 프로세스가 여전히 남아있거나 하는등의 문제입니다..
:
: 왜 저 부분에서 계속해서 멈춰있는지 모르겠습니다...
:
: 1번의 경우는 서버쪽에서 뭔가를 보내줄 수가 없는 상황인 것 같아서 클라이언트 쪽에서 뭔가를 해야 할 것 같은데..
: 다른 방법이 있을런지요...?
:
: 2번의 경우는 전혀 감이 잡히지 않습니다.
:
: 2번 관련 서버 쪽 소스코드를 올리겠습니다.
:
:
: void __fastcall TForm1::IdTCPServer1Execute(TIdContext *AContext)
: {
: TIdBytes tStr;
:
: try
: {
: if(AContext->Connection->Connected())
: {
: try
: {
: tStr.set_length(sizeof(FROMCLIENT));
: int s = sizeof(FROMCLIENT);
:
: memset(&FROMCLIENT,0,sizeof(FROMCLIENT));
: AContext->Connection->IOHandler->ReadBytes(tStr,s,false);//이 부분에 대기중
:
: BytesToRaw(tStr,&FROMCLIENT,s);
: }
: catch(...)
: {
: ListBox1->Items->Insert(0, "Error");
: Shape1->Brush->Color = clRed;
: }
: }
: }
: