WinSock issues

Hey guys I was just wondering how to get winSock to send data over the internet I've tried everything (with the exception of actually forwarding ports) source code is available on request but I won't put it here because I'm not sure if you guys need it because it's basically just initializing a server and client it can do it over the loopback address 127.0.0.1 but across the network it just won't send anything
Any help would be greatly appreciated thanks guys :)
P.S (that actually stands for post script who knew) please don't link me to that bgnet networking PDF I must have read through that about a million times :(
You'll need to post your code.
Okay here's the code:

Server side:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
CChatServer::CChatServer()
{
    cout << "Starting up TCP Chat server\n";
	m_bIsConnected = false;

    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); 

    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;
}

CChatServer::~CChatServer()
{
    closesocket(m_SListenClient);

    WSACleanup();
}

void CChatServer::StartListenClient()
{

    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);

	//AfxBeginThread(ServerRecThread,(void *)m_SClient);

}



int CChatServer::SendMessagePort(string sMessage)
{
		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;

}

int CChatServer::RecClient(SOCKET sRecSocket)
{
    char temp[4096];
	int iStat;
		
    //cout <<inet_ntoa(from.sin_addr) <<":"<<temp<<"\r\n";
		iStat = recv(sRecSocket,temp,4096,0);
		if(iStat == -1)
		{
			m_vClientList.remove(sRecSocket);
			return 1;
		}
		else
		{
			cout <<":"<<temp<<"\n";
			//SendMessagePort(temp);
			return 0;
		}
	return 0;

}


client side:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
CIPMessage::CIPMessage()
{
	m_bIsConnected = false;
}

void CIPMessage::Init(string sIpAddress, int iPort)
{

	m_sServerIPAddress = sIpAddress;
	m_iServerPort = iPort;
	struct hostent *hp;
	unsigned int addr;
	struct sockaddr_in server;
	

	WSADATA wsaData;

	int wsaret=WSAStartup(0x101,&wsaData);


	if(wsaret!=0)
	{
		return;
	}

	conn=socket(AF_INET,SOCK_STREAM,0);
	if(conn==INVALID_SOCKET)
		return;

	addr=inet_addr(m_sServerIPAddress.c_str());
	hp=gethostbyaddr((char*)&addr,sizeof(addr),AF_INET);
	
	if(hp==NULL)
	{
		closesocket(conn);
		return;
	}

	server.sin_addr.s_addr=*((unsigned long*)hp->h_addr);
	server.sin_family=AF_INET;
	server.sin_port=htons(m_iServerPort);
	if(connect(conn,(struct sockaddr*)&server,sizeof(server)))
	{
		closesocket(conn);
		return;	
	}
	m_bIsConnected = true;
	return;
}

CIPMessage::~CIPMessage()
{
	if(m_bIsConnected)
		closesocket(conn);
}

int CIPMessage::SendMessagePort(string sMessage)
{
		int iStat = 0;

		iStat = send(conn,sMessage.c_str(),sMessage.size()+1,0);
		if(iStat == -1)
			return 1;

		return 0;

}

int CIPMessage::RecMessagePort()
{

		char acRetData[4096];
		int iStat = 0;

		iStat = recv(conn,acRetData,4096,0);
		if(iStat == -1)
			return 1;
		cout<<"-->"<<acRetData<<"\n";

		return 0;

}


Thank you for any help you can give
Last edited on
1. Use Winsock 2 in WSAStartUp.
2. 3rd parameter to socket should be filled in, in this case it's IPPROTO_TCP.
3. A tcp client does socket, bind, connect, then send/recv, then closesocket.
4. A tcp server does socket, bind, listen, accept, ...

You really should start with a working example.
Topic archived. No new replies allowed.