Socket on wrong port

From 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
if((sockfd = socket(PF_INET, SOCK_STREAM, 0)) == -1)
		fatal("in socket");
	if(setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int)) == -1)
		fatal("setting socket option SO_REUSEADDR");
	
	host_addr.sin_family = AF_INET;
	host_addr.sin_port = htons(PORT);
	host_addr.sin_addr.s_addr = 0;
	memset(&(host_addr.sin_zero), '\0', 8);

	if(bind(sockfd, (struct sockaddr *)&host_addr, sizeof(struct sockaddr)) == -1)
		fatal("binding to socket");

	if(listen(sockfd, 5) == -1)
		fatal("listening on socket");

	cout << "Server setup was sucessful!" << endl;
	cout << "Waiting for players to connect... " << endl;
	
	//Wait for 2 players to connect
	while(gameInProgress){
		sin_size = sizeof(struct sockaddr_in);
		if(players == 0){
		player1 = accept(sockfd,(struct sockaddr *)&player1_addr, &sin_size);
			if(player1 == -1)
				fatal("accepting connection");
			else
				players++;

			cout << "server: got connection from " 
			<< inet_ntoa(player1_addr.sin_addr) 
			<< " port " << ntohs(player1_addr.sin_port) << endl;

			send(player1, "P1\n", 3, 0);
		}



Code from Client

1
2
3
4
5
6
7
8
9
10
11
	//Attempt to start connection
	string hostIP;
	int hostPort;
	cout << "Please enter your Host's IP address (IPV4): ";
	cin >> hostIP;
	cout << "Please enter the port to use: ";
	cin >> hostPort;
	cout << "Attempting to start a connection..." << endl;
	Player play(hostIP.c_str(),hostPort);
	cout << "Connection was successful!" << endl;
	//Connection was successful 


So the problem is whenever the client connects it doesn't use the port that was inputted by the user. What am I doing wrong?
What does the Player constructor do on line 9? Is it using "hostPort" properly?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
{
	sockfd = socket(AF_INET, SOCK_STREAM,0);
	
	if(sockfd == 0){
		std::cout << "Failed to get client socket..." <<
			"\nTerminating..." << std::endl;
		exit(0);
	}
	bzero((char*) &hostAddr, sizeof(hostAddr));
	hostAddr.sin_family = AF_INET;
	inet_aton((char*)&hostIP,(struct in_addr*)&hostAddr);
        hostAddr.sin_port = htons(hostPort);
	if (connect(sockfd,(struct sockaddr *)&hostAddr,sizeof(hostAddr)) < 0){	 
		std::cout << "Error making a connection..." <<
			"Terminating..." << std::endl;
		exit(0);
	}
	char temp[3];
	read(sockfd,&temp,sizeof(char[3]));
	std::cout << temp[0] << temp[1] << temp[2];


Thank you for the quick response.
(bump)
whenever the client connects it doesn't use the port that was inputted by the user
Why do you think this is the case?

Have you checked the connection with netstat?
Last edited on
Lines 30-33 from the first snippet I posted shows which port they're using. Maybe I'm confused about something? It would show the port that they put in right?
A socket, like a pipe, has two ends. One is on the server and the other on the client. accept() tells you the client details and that's what you're displaying.
Can someone tell me why people aren't able to connect then? I can connect when doing it on localhost but not through the net with an IP address.
Can someone tell me why people aren't able to connect then? Because you're not listening on the correct interface.
Hm, now I'm just confused. This is my first time working with C Sockets. Do you have a link to an article or something that could clarify things for me? Thanks.
One common reference is http://beej.us/guide/bgnet/

Take a look at the use of INADDR_ANY.
through the net

The internet or your lan?
Topic archived. No new replies allowed.