002
25.01.2004, 13:37 Uhr
FloSoft
Medialer Over-Flow (Administrator)
|
Hier ist der Client:
C++: |
#include <windows.h> #include <iostream>
#define SERVER_IP "127.0.0.1" #define SERVER_PORT 20000
int main(void) { WSADATA wsa; static struct sockaddr_in sa; SOCKADDR_IN ca; fd_set wfds; struct timeval tv; // WSAStartup ist obligatorisch if(WSAStartup(MAKEWORD(2,0),&wsa)!=0) { std::cerr << "WSAStartup failure" << std::endl; WSACleanup(); return 1; }
SOCKET Client;
Client = socket(AF_INET,SOCK_STREAM,0); if(Client == INVALID_SOCKET) { std::cerr << "Client Socket creation failed" << std::endl; WSACleanup(); return 1; } sa.sin_family=AF_INET; sa.sin_port=htons(SERVER_PORT); sa.sin_addr.S_un.S_addr=inet_addr(SERVER_IP);
connect(Client,(SOCKADDR*)&sa,sizeof(sa));
for(;;) // Auf Verbindungen warten { // Timeout des Sockets setzen FD_ZERO(&wfds); FD_SET(Client, &wfds); tv.tv_sec = 2; // 2 Sekunden tv.tv_usec = 0; int iSelect = select( ((int)Client)+1, NULL, &wfds, NULL, &tv); // (Parameter: Socket, Readable, Writeable, Error, Timeout) if( SOCKET_ERROR == iSelect ) { std::cerr << "Fehler: select fehlgeschlagen" << std::endl; WSACleanup(); return 1; } if(FD_ISSET(Client,&wfds)) // Wenn Verbindung da ist { for(;;) { char cmd[501]; std::cout << "Verbindung hergestellt, max 500 Zeichen eingeben! (exit zum Beenden)" << std::endl; std::cin.getline(cmd,500); if(strcmp(cmd,"exit")==0) break; std::cout << send(Client,cmd,strlen(cmd),0) << " von " << strlen(cmd) << "Zeichen versendet" << std::endl; } break; } } closesocket(Client); WSACleanup(); return 0; }
|
Alles ungetestet, also falls was nicht funzt ... :):):) -- class God : public ChuckNorris { }; Dieser Post wurde am 25.01.2004 um 13:37 Uhr von FloSoft editiert. |