get ip fra client socket
hejsahar et lille problem.
jeg har laver en dummy af en tilnet server.
som en gang i fremtiden skal kunne modtage fiktive mail's og kunne kontroler om det er spam.
men lige nu sider jeg fast med et forholdsvis simpel problem.
jeg skal have ip på de cliente der connecter.
pt. har jeg lave 2 classe, CServer,CClient
CServer tage sig af serveren, oprettelse af server, lytter til port, opretter threat.
CCleint, tager sig af at modtage og sende data til client.
hvordan får jeg fat i ip på clienten der connecter??
/Taiko_m
main.h, include's
#include <Afxwin.h>
#include <stdio.h>
#include <winsock2.h>
#include <conio.h>
#include <list>
#include <iostream>
#include <cassert>
using namespace std;
--------Main.cpp--------
#include "main.h"
#include "cserver.h"
#include "cclient.h"
//int argc, char* argv[]
UINT ServerRecThread(LPVOID pParam){
#ifdef TEST_MYTELNETSERVER_DEBUG //DEBUG
log("ServerRecThread");
#endif
CClient *pCC = (CClient*)pParam;
pCC->RecClient();
#ifdef TEST_MYTELNETSERVER_DEBUG //DEBUG
log("ServerRecThread stop");
#endif
delete pCC;
return 0;
}
UINT ServerListenThread(LPVOID pParam){
CServer *pCS = (CServer*)pParam;
#ifdef MAIN_MYTELNETSERVER_DEBUG //DEBUG
log("ServerListenThread");
#endif
while(1)
pCS->StartListenClient();
#ifdef MAIN_MYTELNETSERVER_DEBUG //DEBUG
log("ServerListenThread stop");
#endif
return 0;
}
int main(){
#ifdef MAIN_MYTELNETSERVER_DEBUG //DEBUG
log("MAIN");
#endif
CServer CServerObj;
char cBuf[4096];
if(!CServerObj.IsConnected())
cout << "ERROR(1)" << endl;
AfxBeginThread(ServerListenThread,&CServerObj);
while(gets(cBuf)){
cout << strlen(cBuf);
if(strlen(cBuf)==0)
break;
if(CServerObj.SendMessagePort(cBuf)){
cout << "Error" << endl;
break;
}
}
getch();
system("pause");
return 0;
}
void log(string str){
cout << str.c_str() << endl;
}
------------CServer.cpp----------------------------
#include "cserver.h"
#include "cclient.h"
#include "main.h"
CServer::CServer(){
#ifdef CSERVER_MYTELNETSERVER_DEBUG //DEBUG
log("CServer::Constroktor");
#endif
cout << "Starting up TCP Chat server\n";
m_bIsConnected = false;
m_iPort=23;
WSADATA wsaData;
sockaddr_in local;
int wsaret=WSAStartup(0x101,&wsaData);
if(wsaret!=0)
{
return;
}
local.sin_family=AF_INET;
local.sin_addr.s_addr=INADDR_ANY;
//local.sin_port=htons((u_short)8084);
local.sin_port=htons((u_short)m_iPort);
m_SListenClient=socket(AF_INET,SOCK_STREAM,0);
if(m_SListenClient==INVALID_SOCKET)
{
return;
}
if(bind(m_SListenClient,(sockaddr*)&local,sizeof(local))!=0)
{
return;
}
if(listen(m_SListenClient,10)!=0)
{
return;
}
m_bIsConnected = true;
return;
}
CServer::~CServer(){
#ifdef CSERVER_MYTELNETSERVER_DEBUG //DEBUG
log("CServer::Destroktor");
#endif
closesocket(m_SListenClient);
WSACleanup();
}
int CServer::SendMessagePort(string sMessage){
#ifdef CSERVER_MYTELNETSERVER_DEBUG //DEBUG
log("CServer::SendMessagePort");
#endif
int iStat = 0;
list<SOCKET>::iterator itl;
if(m_vClientList.size() == 0)
return 0;
for(itl = m_vClientList.begin();itl != m_vClientList.end();itl++)
{
iStat = send(*itl,sMessage.c_str(),sMessage.size()+1,0);
if(iStat == -1)
m_vClientList.remove(*itl);
}
if(iStat == -1)
return 1;
return 0;
}
void CServer::StartListenClient(){
#ifdef CSERVER_MYTELNETSERVER_DEBUG //DEBUG
log("CServer::StartListenClient");
#endif
SOCKET m_SClient; // skal rettes
sockaddr_in from;
int fromlen=sizeof(from);
m_SClient=accept(m_SListenClient,
(struct sockaddr*)&from,&fromlen);
if(m_SClient != INVALID_SOCKET)
m_vClientList.push_back(m_SClient);
CClient *pCClient = new CClient(m_SClient);
AfxBeginThread(ServerRecThread,(void *)pCClient); // skal rettes
}