Strange Winsock2 Results

I wrote a mini remote administration tool. The program conists of a client and server.

1.The server accepts the connection of the client
2.The server && client std::couts's the IP and PORT number.
3.The server then sends a command to the client.
4.The client executes that command using popen and system() (I know this is bad, I just want this program as an example!)
5.The client then sends the results back to the server.
6.goto 2

First of all i never got this program to run so maybe there is more then this one problem going on. The problem is that the server always receives a connection from IP: 204.204.204.204 && PORT: 52428. I did some research and i found out that 204 is 0xCC, which is the value that the compiler uses for
uninitialized stack variables in debug builds to help catch bad
accesses. And 52428 equals 0xCCCC, Again uninitialized. 2 Days ago i had a similar program that was just a chat program were you needed to wait for a answer before you could send a message back. This worked flawlessly. Then i modified the code and now it doesn't work any more.

Before i post the code i know it is bad because i dont use error handling. But for this small example i decided that this should do the job. Since a earlier version worked flawlessly.

Here is the source for the CLIENT.

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
#include<winsock2.h>
#include<windows.h>
#include<iostream>
#include<sstream>
#include<string>


std::string sys_com (char syscom[128])
{
	std::stringstream stream;
	char   psBuffer[128];
	FILE   *pPipe;

	if( (pPipe=_popen(syscom, "rt"))==NULL )
      exit( 1 );

	while(fgets(psBuffer,128,pPipe))
	{
		stream<<psBuffer;
	}
	return stream.str();
}

void main(void)
{
	WSADATA wsaData;
	WSAStartup(MAKEWORD(2,0),&wsaData);

	SOCKET cSocket=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);

	sockaddr_in sin;
	sin.sin_family=AF_INET;
	sin.sin_port=htons(4030);
	sin.sin_addr.S_un.S_addr=inet_addr("127.0.0.1");

	connect(cSocket,(sockaddr*)(&sin),sizeof(sin));

	std::cout<<"IP:"<<inet_ntoa(sin.sin_addr)<<std::endl<<"PORT:"<<ntohs(sin.sin_port)<<std::endl<<std::endl;

	while(true)
	{
		char buffer[128];
		recv(cSocket,buffer,128,0);

		std::string s_result=sys_com(buffer);

		std::stringstream stream;
		stream<<s_result;
		char response[128];
		stream>>response;
		std::cout<<std::endl<<response<<std::endl;
		system("pause");
		send(cSocket,response,128,0);
	}
}


Last but not least the code for the server.

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
#include<WinSock2.h>
#include<windows.h>
#include<iostream>
#include<sstream>
#include<string>

void main(void)
{
	WSADATA wsaData;
	WSAStartup(MAKEWORD(2,0),&wsaData);

	SOCKET sSocket,cSocket;
	sSocket=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
	cSocket=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);

	sockaddr_in ssin,csin;

	ssin.sin_family=AF_INET;
	ssin.sin_port=htons(4030);
	ssin.sin_addr.S_un.S_addr=inet_addr("127.0.0.1");

	int csin_Size=sizeof(csin);

	bind(sSocket,reinterpret_cast<sockaddr*>(&ssin),sizeof(ssin));

	listen(sSocket,SOMAXCONN);
	
	accept(cSocket,reinterpret_cast<sockaddr*>(&csin),&csin_Size);

	std::cout<<"IP:"<<inet_ntoa(csin.sin_addr)<<std::endl<<"PORT:"<<ntohs(csin.sin_port)<<std::endl<<std::endl;

	while(true)
	{
		std::string com;
		std::getline(std::cin,com);

		std::stringstream stream;
		stream<<com;
		char command[128];
		stream>>command;

		send(cSocket,command,128,0);
		char response[128];
		recv(cSocket,response,128,0);

		std::cout<<response<<std::endl;
	}
}


Thanks in advance!

EDIT----------------------------------------------------------------------------EDIT


I created another client. It always connects to something no matter wat port i give it. Before running the program i did a netstat -a to see if anything was listhening on my specefied port. The program outputs

Connected With IP 127.0.0.1 on port 12345

But there was nothing listhening i did not had any server program running here is the netstat -a result before running the program.

Active Connections

Proto Local Address Foreign Address State
TCP pc:epmap pc:0 LISTENING
TCP pc:microsoft-ds pc:0 LISTENING
TCP pc:1026 pc:0 LISTENING
TCP pc:1035 localhost:1036 ESTABLISHED
TCP pc:1036 localhost:1035 ESTABLISHED
TCP pc:1037 localhost:1038 ESTABLISHED
TCP pc:1038 localhost:1037 ESTABLISHED
TCP pc:netbios-ssn pc:0 LISTENING
TCP pc:2406 hamburg.ebuddy.com:http ESTABLISHED
TCP pc:3023 gru03s06-in-f7.1e100.net:http TIME_WAIT
TCP pc:3295 gru03s06-in-f25.1e100.net:http ESTABLISHED
TCP pc:3297 gru03s06-in-f25.1e100.net:http ESTABLISHED
TCP pc:3305 gru03s06-in-f5.1e100.net:http ESTABLISHED
TCP pc:3328 hzs5.cnzz.com:http ESTABLISHED
UDP pc:microsoft-ds *:*
UDP pc:isakmp *:*
UDP pc:4500 *:*
UDP pc:ntp *:*
UDP pc:1025 *:*
UDP pc:1900 *:*
UDP pc:ntp *:*
UDP pc:netbios-ns *:*
UDP pc:netbios-dgm *:*
UDP pc:1900 *:*
UDP pc:ntp *:*
UDP pc:1900 *:*


here is the netstat -a after running the program (while it was connected)

Active Connections

Proto Local Address Foreign Address State
TCP pc:epmap pc:0 LISTENING
TCP pc:microsoft-ds pc:0 LISTENING
TCP pc:1026 pc:0 LISTENING
TCP pc:1035 localhost:1036 ESTABLISHED
TCP pc:1036 localhost:1035 ESTABLISHED
TCP pc:1037 localhost:1038 ESTABLISHED
TCP pc:1038 localhost:1037 ESTABLISHED
TCP pc:netbios-ssn pc:0 LISTENING
TCP pc:2406 hamburg.ebuddy.com:http ESTABLISHED
UDP pc:microsoft-ds *:*
UDP pc:isakmp *:*
UDP pc:4500 *:*
UDP pc:ntp *:*
UDP pc:1025 *:*
UDP pc:1900 *:*
UDP pc:ntp *:*
UDP pc:netbios-ns *:*
UDP pc:netbios-dgm *:*
UDP pc:1900 *:*
UDP pc:ntp *:*
UDP pc:1900 *:*


Wat mistake am i making i am confused! Here is the new clients source:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include<WinSock2.h>
#include<Windows.h>
#include<iostream>

int main(void)
{
	WSADATA wsaData;
	if(WSAStartup(MAKEWORD(2,0),&wsaData)!=0)
		return 1;

	sockaddr_in ClientSockAddr;
	ClientSockAddr.sin_family=AF_INET;
	ClientSockAddr.sin_port=htons(12345);
	ClientSockAddr.sin_addr.S_un.S_addr=inet_addr("127.0.0.1");

	SOCKET ClientSocket=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);

	connect(ClientSocket,(sockaddr*)(&ClientSockAddr),sizeof(ClientSockAddr));

	std::cout<<"Connected With IP "<<inet_ntoa(ClientSockAddr.sin_addr)<<" on port "<<ntohs(ClientSockAddr.sin_port)<<'\n';

	system("pause");
}



EDIT----------------------------------------------------------------------------EDIT
Last edited on
shameless self bump. It is still not fixed. I am getting crazy!
Topic archived. No new replies allowed.