PhonniX Temat założony przez niniejszego użytkownika |
[Winsock] Aplikacja Klient - Serwer, problem z przesyłaniem wiadomości » 2009-10-03 09:27:19 Witam. Piszę taki swój mini chat, zrobiłem serwer i klienta, serwer wysyła wiadomość, ale w kliencie nie widać tej wiadomości (nie odbiera go). SERWER: #include <winsock2.h> #include <windows.h> #include <stdio.h> #include <stdlib.h> #include <conio.h> #include <iostream> #include <string>
#pragma comment(lib, "ws2_32.lib")
#define DEFAULT_PORT 5150 #define DEFAULT_BUFFER 64
using namespace std;
int iPort = DEFAULT_PORT;
int main( int argc, char ** argv ) { cout << "===================================" << endl; cout << "===== Serwer =====" << endl; cout << "===================================" << endl; WSADATA wsd; SOCKET sListen, sClient; int iAddrSize; sockaddr_in local, client; int iPort = DEFAULT_PORT; int fdmax; int newfd; fd_set master; fd_set read_fds; FD_ZERO( & master ); FD_ZERO( & read_fds ); int i, j; char buf[ 256 ]; int nbytes; int yes = 1; int addrlen; struct sockaddr_in remoteaddr; char welcome[ DEFAULT_BUFFER ]; if( WSAStartup( MAKEWORD( 2, 2 ), & wsd ) != 0 ) { cout << "Nie mozna stworzyc WinSock'a." << endl; return 1; } sListen = socket( AF_INET, SOCK_STREAM, IPPROTO_IP ); if( sListen == SOCKET_ERROR ) { cout << "Nie mozna stworzyc gniazda (" << WSAGetLastError() << ")." << endl; return 1; } local.sin_addr.s_addr = htonl( INADDR_ANY ); local.sin_family = AF_INET; local.sin_port = htons(( u_short ) iPort ); if( bind( sListen,( sockaddr * ) & local, sizeof( local ) ) == SOCKET_ERROR ) { cout << "Nie mozna przypisac ustawien (" << WSAGetLastError() << ")." << endl; return 1; } listen( sListen, SOMAXCONN ); FD_SET( sListen, & master ); fdmax = sListen; cout << "Serwer uruchomiony pomyslnie." << endl; while( true ) { read_fds = master; if( select( fdmax + 1, & read_fds, NULL, NULL, NULL ) == - 1 ) { perror( "select" ); exit( 1 ); } for( i = 0; i <= fdmax; i++ ) { if( FD_ISSET( i, & read_fds ) ) { if( i == sListen ) { addrlen = sizeof( remoteaddr ); if(( newfd = accept( sListen,( struct sockaddr * ) & remoteaddr, & addrlen ) ) == - 1 ) { perror( "accept" ); } else { FD_SET( newfd, & master ); if( newfd > fdmax ) { fdmax = newfd; } cout << "Serwer: Nowe polaczenie z " << inet_ntoa( remoteaddr.sin_addr ) << " na " << newfd << endl; } } else { if(( nbytes = recv( i, buf, sizeof( buf ), 0 ) ) <= 0 ) { if( nbytes == 0 ) { cout << "Serwer: socket " << i << " rozłączył sie" << endl; } else { perror( "recv" ); } FD_CLR( i, & master ); } else { for( j = 0; j <= fdmax; j++ ) { if( FD_ISSET( j, & master ) ) { if( j != sListen && j != i ) { if( send( j, buf, nbytes, 0 ) == - 1 ) { perror( "send" ); } } } } } } } } char welcome[ DEFAULT_BUFFER ] = "Witam na przykladowym serwerze!"; send( sClient, welcome, strlen( welcome ) + 1, 0 ); cout << "Wyslano: " << welcome << endl; recv( sClient, welcome, DEFAULT_BUFFER, 0 ); cout << "Odebrano: " << welcome << endl; } cout << "Rozlaczam."; closesocket( sClient ); closesocket( sListen ); WSACleanup(); cout << "Serwer wylaczony." << endl; }
KLIENT: #include <winsock2.h> #include <windows.h> #include <stdio.h> #include <stdlib.h> #include <conio.h> #include <iostream>
#pragma comment(lib, "ws2_32.lib")
#define DEFAULT_PORT 5150 #define DEFAULT_BUFFER 64
using namespace std;
int main( int argc, char ** argv ) { cout << "===================================" << endl; cout << "===== Klient =====" << endl; cout << "===================================" << endl; WSADATA wsd; SOCKET sClient; sockaddr_in server; hostent * host = NULL; char szServer[ 128 ]; int iPort = DEFAULT_PORT; cout << "Podaj IP: "; cin >> szServer; if( WSAStartup( MAKEWORD( 2, 2 ), & wsd ) != 0 ) { cout << "Nie mozna stworzyc WinSock'a." << endl; return 1; } sClient = socket( AF_INET, SOCK_STREAM, IPPROTO_TCP ); if( sClient == INVALID_SOCKET ) { cout << "Nie mozna utworzyc gniazda (" << WSAGetLastError() << ")." << endl; return 1; } server.sin_family = AF_INET; server.sin_port = htons(( u_short ) iPort ); server.sin_addr.s_addr = inet_addr( szServer ); if( server.sin_addr.s_addr == INADDR_NONE ) { host = gethostbyname( szServer ); if( host == NULL ) { cout << "Nie mozna przeksztalcic na ip: " << szServer << endl; return 1; } CopyMemory( & server.sin_addr, host->h_addr_list[ 0 ], host->h_length ); } if( connect( sClient,( sockaddr * ) & server, sizeof( server ) ) == SOCKET_ERROR ) { cout << "Nie mozna sie polaczyc (" << WSAGetLastError() << ")." << endl; return 1; } cout << "Polaczono." << endl; char buffer[ DEFAULT_BUFFER ]; recv( sClient, buffer, DEFAULT_BUFFER, 0 ); cout << "Odebrano: " << buffer << endl; strcpy( buffer, "Witam serdecznie serwerka :)" ); send( sClient, buffer, strlen( buffer ) + 1, 0 ); cout << "Wyslano: " << buffer << endl; cout << "Rozlaczam." << endl; closesocket( sClient ); WSACleanup(); return 0; }
|
jimmye |
» 2009-10-03 11:36:20 Z pewnego artykułu o WinSock wynika, że powinieneś robić to inaczej :P Przykład: read_fds = master; if( select( fdmax + 1, & read_fds, NULL, NULL, NULL ) == - 1 ) { perror( "select" ); exit( 1 ); }
for( i = 0; i <= fdmax; i++ ) { if( FD_ISSET( i, & read_fds ) ) { if( i == listener ) { } else { if(( nbytes = recv( i, buf, sizeof( buf ), 0 ) ) <= 0 ) { if( nbytes == 0 ) { printf( "selectserver: socket %d hung up\n", i ); } else { perror( "recv" ); } close( i ); FD_CLR( i, & master ); } else { } } } }
|