009
22.05.2006, 14:25 Uhr
Ganael
|
puh, I find the error myself... my client couldn't send because I thought I must use the Server Socket in the send() function! there should be the socket of the client himself!
...as I started with sockets I saw that the Server was using the Client socket to send to the client, so I thought the Client should use the Server socket to send to the server...
...thats why I send the server socket int from the server to the client...
here the correct code :
erstmal mein Socket.h :
C++: |
//Socket init
SOCKET sock; //variable du type Socket SOCKADDR_IN sin; SOCKET csock; SOCKADDR_IN csin; //informations techniques du socket int port = 50;//2388 char hostIP[]="127.0.0.1";//"62.214.238.147"
//CLIENT void openclientsock () { WSADATA WSAData; //Windows Socket Data, ou un truc du genre... WSAStartup(MAKEWORD(2,0),&WSAData); //la version utilisée, ici v.2
sock = socket(AF_INET,SOCK_STREAM,0); //Cré un Socket appelé "sock" sin.sin_addr.s_addr = inet_addr(hostIP); //l'addresse du server sin.sin_family = AF_INET; //type de Socket sin.sin_port = htons(port); //Port }
//SERVER void openserversock () { WSADATA WSAData; WSAStartup(MAKEWORD(2,0),&WSAData);
sock = socket(AF_INET, SOCK_STREAM, 0);
sin.sin_addr.s_addr = INADDR_ANY; sin.sin_family = AF_INET; sin.sin_port = htons(port);
bind(sock, (SOCKADDR *)&sin, sizeof(sin)); }
//CLOSE SOCKET void closesock () { closesocket(sock); //Protokol qui ferme le socket... WSACleanup(); //..et le WSA }
//EMPFANGEN char* reccord() { char buffer[1024]={0}; int bytes; bytes = recv(csock, buffer, sizeof(buffer) - 1, 0); if (bytes == -1) { //cout << "\n !couldn't retreave the message!\n"; system ("pause"); } else if(bytes >0) { buffer[bytes] = '\0'; cout << "\nreceived message : "<<buffer <<endl; } return (buffer); }
//Senden void sende(const char* mess) { int bytes; bytes = send(csock,mess,strlen(mess),0); if (bytes == -1) { system ("cls"); cout << "\n !couldn't send the message!\n"; } else { cout << "\nmessage send : "<<mess <<endl; } }
|
und hier das Programm selbst :
C++: |
#include <iostream> #include <stdio.h> #include <conio.h> #include <winsock2.h> #include <cstdlib> #include <string> #pragma comment(lib, "ws2_32.lib") using namespace std;
#include "Socket.h"
void main() {
int g=0,conn=0;
while (1!=g && 2!=g) { cout << "please choose between\n "; cout << "1. Open Client\n 2. Open Server\n"; cin >> g; }
if (1==g || 2==g) { switch (g){ case 1: cout << "Client activated\n"; openclientsock();break; case 2: cout << "Server activated\n"; openserversock();break; } }
//client if (g==1) { csock=sock; connect(sock,(SOCKADDR *)&sin,sizeof(sin)); reccord(); if( sock != INVALID_SOCKET) { cout << "connection engaged\n"; sende("Hi I am the Client\0"); reccord(); } }
//server if (g==2) { listen(sock, 0);
int sinsize = sizeof(csin); csock = accept(sock,(SOCKADDR *)&csin,&sinsize); if( csock != INVALID_SOCKET) { int dec, sign; string Port = fcvt (port,0, &dec, &sign ); string HI = "hi, you are now conected at "; string HI_IP_Port = HI + hostIP + " on port : " + Port;
sende(HI_IP_Port.c_str()); cout << "connection engaged\n"; } //get messages --> //dialogue while (true) { char* recmessage=reccord(); if (strlen(recmessage)>0) { char message[]="Hallo Client"; sende(message); } } }
closesock(); system ("pause"); }
|
|