C++ Klient/server ihopkoppling med winsock|Programmering / Grafisk formgivning|Forum|Nordichardware

Search
Forum Scope


Match



Forum Options



Minimum search word length is 3 characters - maximum search word length is 84 characters
Lost password?
The forums are currently locked and only available for read only access
sp_Feed sp_TopicIcon
C++ Klient/server ihopkoppling med winsock
ULT
Siktar mot toppen
Medlem
Forum Posts: 97
Member Since:
October 29, 2003
sp_UserOfflineSmall Offline
1
January 30, 2007 - 12:45 pm
sp_Permalink sp_Print

Hej jag min polare har precis fått en uppgift att med hjälp av två nästan fullständiga program få det ena som är en klient att ansluta till det andra som agerar server. Skulle någon kanske kunna kolla igenom dessa program och ge oss synpunkter då vi helt kört fast.

Klienten:

#define WIN32_LEAN_AND_MEAN
#pragma comment(lib, "wsock32.lib") // eller obs komm ihåg att länka wsock32.lib
#include
#include
#include
using namespace std;

int main()
{
char szSmtpServerName[64] = "";
char szToAddr[64] = "";
char szFromAddr[64] = "";
char TextIn[4096] = "";
char szLine[255] = "";
char TextOut[255] = "";
SOCKET hServer;
WSADATA WSData;
LPHOSTENT lpHostEntry;
SOCKADDR_IN SockAddr;

cout << "Server namn :" ;
cin >> TextIn;
strcpy (szSmtpServerName,TextIn);
int port;
cout << "Port :" ;
cin >> port;

// Initiera WinSock (1.1 eller senare)
if(WSAStartup(MAKEWORD(1, 1), &WSData))
{
cout << "Kan inte hitta v" << 1 << "." << 1 << " eller senare!" << endl;
return 1;
}

//hämta eget namn
char namn[25];
gethostname(namn, sizeof(namn));
cout << namn; // visa egen IP adress
PHOSTENT hostinfo;
hostinfo = gethostbyname(namn);
cout << endl << inet_ntoa (*(struct in_addr *)*hostinfo->h_addr_list) << endl; // hämta serverns IP adress.
lpHostEntry = gethostbyname(szSmtpServerName);
if(!lpHostEntry)
{
cout << "Kan inte hitta servern : " << szSmtpServerName << endl;
return 1;
}

// Skapa TCP/IP sockeln
hServer = socket(PF_INET, SOCK_STREAM, 0);
if(hServer == INVALID_SOCKET)
{
cout << "Kan inte öppna mail server sockeln" << endl;
return 1;
}

// Skapa Sockel Adress struktur
SockAddr.sin_family = AF_INET;
SockAddr.sin_port = htons(port);
SockAddr.sin_addr = *((LPIN_ADDR)*lpHostEntry->h_addr_list);

// Ansluta till sockeln
if(connect(hServer, (PSOCKADDR) &SockAddr, sizeof(SockAddr)))
{
cout << "Fel med anslutning till Server sockeln" << endl;
return 1;
}

char Text[255];
while (1)
{
recv(hServer, TextIn, sizeof(TextIn), 0);
cout << endl << TextIn << endl;
cout << endl << "Skicka : ";
cin >> Text;
send(hServer, Text, strlen(Text), 0);
strcpy(Text," ");
strcpy(TextIn," ");
}
closesocket(hServer);

WSACleanup();
system ("pause");
return 0;
} [SOURCE=cpp][/SOURCE]

Servern:

#define WIN32_LEAN_AND_MEAN // obs komm ihåg att länka wsock32.lib

#include
#include
#include
#pragma comment(lib, "wsock32.lib")
using namespace std;

int MAX_CONNECTS=5, PORT=1234,SO_MAXCONN=5, ClientID;
bool ThreadInit = false;
struct CLIENTS {
bool InUse;
SOCKET ClientSocket;
sockaddr_in ClientAddress;
DWORD* dwThreadID;
HANDLE hThreadID;
};
SOCKET SinkSocket;
sockaddr_in SinkSockAddr;
CLIENTS Clients[ 5 ];
SOCKET ListeningSocket;
sockaddr_in Address;

HRESULT InitWinSock( );
HRESULT StartClientThread( );
DWORD WINAPI ClientThreadEntry( LPVOID Arg1 );
main()
{
InitWinSock();
ListeningSocket = socket( AF_INET, SOCK_STREAM, 0 );

Address.sin_family = AF_INET;
Address.sin_port = htons( PORT );
Address.sin_addr.s_addr = htonl( INADDR_ANY );

bind( ListeningSocket, (LPSOCKADDR)&Address, sizeof(Address));

listen( ListeningSocket, SO_MAXCONN );

StartClientThread( );
Clients[ClientID].hThreadID = CreateThread( NULL, 0, &ClientThreadEntry, 0, 0, Clients[ClientID].dwThreadID );

return 0;
}

HRESULT InitWinSock( )
{
WSADATA wsad;

for( int i = 0; i < MAX_CONNECTS; i++ )
{
Clients[i].InUse = false;
}

WSAStartup( MAKEWORD( 2, 2 ), &wsad );

return S_OK;
}
HRESULT StartClientThread( ){
// NO ONE ELSE CAN CONNECT UNTIL THIS THREAD IS READY
ThreadInit = true;

for( int i = 0; i < 5; i++ ){
if( Clients[i].InUse == true ){
ClientID = i;
break;
}
}
return 0;
}

DWORD WINAPI ClientThreadEntry( LPVOID Arg1 ){
int ClientNo = ClientID;
CLIENTS *Client = &Clients[ ClientID ];

Client->InUse = true;

// MOVE THE SOCKET INFORMATION FROM SOCK A TO SOCKET B
memcpy( &Client->ClientSocket, &SinkSocket, sizeof( SinkSocket ));
memcpy( &Client->ClientAddress, &SinkSockAddr, sizeof( SinkSockAddr ));

// DONE .. CONTINUE NORMAL EXECUTION IN THE PARENT THREAD
ThreadInit = false;

return 0;
}

//Vi har fått klienten att fungera i sig men inte att få den att ansluta till servern. Väldigt tacksam för svar

// Jonas

Forum Timezone: Europe/Stockholm
Most Users Ever Online: 1030
Currently Online:
Guest(s) 384
Currently Browsing this Page:
1 Guest(s)
Top Posters:
Andreas Galistel: 16287
Jonas Klar: 15897
ilg@dd: 10810
Nyhet: 10607
Mind: 10550
Ctrl: 10355
Gueno: 9881
Guest: 9344
Snorch: 8881
Callister: 8468
Newest Members:
PetrbonFU PetrbonFU
Karine Bembry
Dolores Mcdaniels
Anibal McLeish
Francisca Alt
Alfie Everhart
Lester Huitt
Orlando Jorgensen
Mikki Lundgren
Dakota Kozlowski
Forum Stats:
Groups: 11
Forums: 59
Topics: 146630
Posts: 1300967

 

Member Stats:
Guest Posters: 2
Members: 79425
Moderators: 0
Admins: 11
Administrators: nordicadmin, Henrik Berntsson, Anton Karmehed, Carl Holmberg, Joel Oscarsson, Mikael Linnér, Mikael Schwartz, Andreas Paulsson, Nickebjrk, Mattias Pettersson, EmxL