Witam.
Zachcialo mi sie napisac "liba" do TCP w C, tyle o ile potrafie przeslac sobie tekst od serwera echo ( w celach testow : ) ), tyle z struct jest problem, niby wysyla ... niby odbiera ale cos danych nie mam : /
Serwer:
Kod:
#include <stdio.h>
#include <winsock.h>
int main( void )
{
WSADATA wsd;
if( WSAStartup(MAKEWORD(2,2), &wsd ) != 0 )
{
return -1;
}
SOCKET SerwerSocket;
SerwerSocket = socket( AF_INET, SOCK_STREAM, 0 );
if( SerwerSocket == INVALID_SOCKET )
{
return -1;
}
else
{
sockaddr_in SerwerAddres;
SerwerAddres.sin_family = AF_INET;
SerwerAddres.sin_port = htons( 2000 );
SerwerAddres.sin_addr.s_addr = htonl( INADDR_ANY );
memset( SerwerAddres.sin_zero, 0, 8 );
if( bind( SerwerSocket, (struct sockaddr *)&SerwerAddres, sizeof( SerwerAddres ) ) == SOCKET_ERROR )
{
return -1;
}
else
{
listen( SerwerSocket, 8 );
int ClientAddresLenght;
sockaddr_in ClientAddres;
SOCKET ClientSocket;
while( 1 )
{
ClientAddresLenght = sizeof( ClientAddres );
ClientSocket = accept( SerwerSocket, (struct sockaddr *)&ClientAddres, &ClientAddresLenght );
if( ClientSocket == INVALID_SOCKET )
{
return 1;
}
else
{
int Error = 0;
char RecvBuff[300];
while( Error != SOCKET_ERROR )
{
memset( RecvBuff, '\0', sizeof(RecvBuff) );
Error = recv( ClientSocket, RecvBuff, sizeof( RecvBuff ), 0 );
printf( "Data recv: %d\n", Error );
Error = send( ClientSocket, RecvBuff, sizeof( RecvBuff ), 0 );
}
closesocket( ClientSocket );
}
}
}
}
return 0;
}
Klient ( ak'a lib ):
Kod:
#include <stdio.h>
#include <winsock.h>
int TcpClientStart( void );
int TcpClientStop( void );
int TcpClientSendText( char *SendMsg );
int TcpClientRecvText( char *RecvMsg, int RecvMsgSize );
// Nie dziala ... ; /
int TcpClientSendData( void *SendData, int SendDataSize );
int TcpClientRecvData( void *RecvData, int RecvDataSize );
unsigned int TcpClientSocket; // Gniazdo
sockaddr_in TcpClientAddr; // Adres
int main( void )
{
/* Start */
TcpClientStart();
/* Test 1 - start*/
TcpClientSendText( "To jest Test" );
char Buffor[20];
memset( Buffor, '\0', sizeof( Buffor ) );
TcpClientRecvText( Buffor, sizeof( Buffor ) );
printf("Data: %s\n", Buffor );
/* Test 1 - stop*/
/* Test 2 - start*/
struct TestStruct
{
int TestVal;
char TestChar[20];
};
TestStruct Test_1;
TestStruct Test_2;
Test_1.TestVal = 10;
memset( Test_1.TestChar, '\0', sizeof( Test_1.TestChar ) );
strcpy( Test_1.TestChar, "To jest Test" );
TcpClientSendData( &Test_1, sizeof( TestStruct ) );
TcpClientRecvData( &Test_2, sizeof( TestStruct ) );
printf( "Data: %d\n", Test_2.TestVal );
printf( "Data: %s\n", Test_2.TestChar );
/* Test 2 - stop*/
/* Stop*/
TcpClientStop();
return 0;
}
int TcpClientStart( void )
{
WSADATA wsd;
if( WSAStartup(MAKEWORD(2,2), &wsd ) != 0 )
{
return -1;
}
TcpClientSocket = 0;
TcpClientSocket = socket( AF_INET, SOCK_STREAM, 0 );
if( TcpClientSocket == (unsigned int)-1 )
{
return -1;
}
TcpClientAddr.sin_family = AF_INET;
TcpClientAddr.sin_port = htons( 2000 );
unsigned long addr = inet_addr( "localhost" );
if( addr == (unsigned int)-1 )
{
hostent *HE = gethostbyname( "localhost" );
if( !HE )
{
return -1;
WSACleanup();
}
addr = *( (u_long *)HE->h_addr_list[0] );
}
TcpClientAddr.sin_addr.s_addr = addr;
memset( TcpClientAddr.sin_zero, 0, 8 );
int Error = connect( TcpClientSocket, (struct sockaddr *)&TcpClientAddr, sizeof( TcpClientAddr ) );
if( Error == -1 )
{
return -1;
}
return 0;
}
int TcpClientStop( void )
{
closesocket( TcpClientSocket );
WSACleanup();
return 0;
}
int TcpClientSendText( char *SendMsg )
{
return ( send( TcpClientSocket, SendMsg, strlen( SendMsg ), 0 ) );
}
int TcpClientRecvText( char *RecvMsg, int RecvMsgSize )
{
return ( recv( TcpClientSocket, RecvMsg, RecvMsgSize, 0 ) );
}
int TcpClientSendData( void *SendData, int SendDataSize )
{
return ( send( TcpClientSocket, (char*)&SendData, SendDataSize, 0 ) );
}
int TcpClientRecvData( void *RecvData, int RecvDataSize )
{
int lenght = 0;
lenght = recv( TcpClientSocket, (char*)RecvData, RecvDataSize, 0 );
printf( "Recv size: %d\n", lenght );
printf( "Recv in: %d\n", RecvDataSize );
return 0;
}
Jakies pomysly ?