I am doing socket programming in C, and when i am starting my tcpserver on address INADDR_ANY and port no 2000, it starts well. But using command 'netstat -tulpn' , it shows the server is assigned port number 53255. Clients are able to connect to this server when they connect using server port number 53255, but get connection refusals when they try connect to port number '2000'.
Can somebody pls explain why the system is assigning the wrong port no to my tcpserver instead of one i am want to use which is 2000.
vm#vm:~/Documents/csepracticals/webserver/TCPWebserver$ netstat -tulpn
...
...
tcp 0 0 0.0.0.0:53255 0.0.0.0:* LISTEN 16291/webserver`
server_addr.sin_family = AF_INET;
server_addr.sin_port = SERVER_PORT; /*#defined to 2000*/
server_addr.sin_addr.s_addr = INADDR_ANY;
bind(master_sock_tcp_fd, (struct sockaddr *)&server_addr, sizeof(struct sockaddr));
You should use htons(SERVER_PORT) instead of SERVER_PORT
Related
I'm working with TCP servers. Let's say I have a server running with a specific port, but then I want to connect a client to it, I would simply go through the typical procedure of socket, bind, listen, accept for the server and then socket, connect for the client. So let's say our server port is 4000, and our client port 4001. Now, I want to create a new client that will connect to my client on port 4001, but to my limited understanding, I cannot do this as a client. Port 4001 would have to pertain to a server and not a client (i.e. it would have to be listening). The issue arises because I don't think you can use the same port for both the server and client.
I've decided to attempt this through the sample code I've provided below. I call the program on the command line as follows:
If this is the first call of the server, then I simply call the program without any arguments and it will automatically run on port 3000. i.e. ./serverprogram
If I would like to connect a client on port 3001 to our server on port 3000. Then I would call the command line with two arguments, the first being 3001 and the second being 3000. i.e. ./serverprogram 3001 3000
#define PORT 3000
int main (int argc, char * argv[]){
int sfd = socket(AF_INET, SOCK_STREAM, 0);
int my_port = (argc == 3) ? atoi(argv[1]) : PORT;
if (argc > 2){
struct sockaddr_in c_addr;
c_addr.sin_family = AF_INET;
memset(&c_addr.sin_zero, 0, 8);
c_addr.sin_port = htons(atoi(argv[2]));
struct addrinfo *result = NULL;
getaddrinfo("AcaciaLinux", NULL, NULL, &result);
struct sockaddr_in *x = (struct sockaddr_in*) result->ai_addr;
c_addr.sin_addr = x->sin_addr;
freeaddrinfo(result);
if(connect(sfd, (struct sockaddr *) &c_addr, sizeof(struct sockaddr_in)) == -1){
perror("connect");
exit(1);
}
printf("We have connected to a server.");
}
if (sfd == -1){
perror("socket");
exit(1);
}
struct sockaddr_in saddr;
saddr.sin_family = AF_INET;
saddr.sin_port = htons(my_port);
saddr.sin_addr.s_addr = INADDR_ANY;
memset(&(saddr.sin_zero), 0, 8);
if(bind(sfd, (struct sockaddr*) &saddr, sizeof(struct sockaddr_in)) == -1){
perror("bind");
close(sfd);
exit(1);
}
if (listen(sfd, 5) < 0){
perror("listen");
exit(1);
}
struct sockaddr_in caddr;
saddr.sin_family = AF_INET;
int cfd;
unsigned int c_len = sizeof(struct sockaddr_in);
if ((cfd = accept(sfd, (struct sockaddr*) &caddr, &c_len)) == -1){
perror("accept");
exit(1);
}
printf("Alas, we have finally connected to a client.");
return 0;
}
Upon running the second instance of the program I receive the error "bind: Invalid argument". I am assuming that this is due to the fact that the port is already in use. Is there any way to bypass this, or is there any way to connect a server to a client, and allow the client to also act as a server using the same port
You cannot open a socket which can do the both listen and connect.
A TCP connection is identified by its two endpoints. Each of those, in turn, is identified by an (IP address, port) pair. Therefore, you cannot simultaneously have two distinct connections between the same two IP addresses with the same ports on each end -- if all of those properties are the same, then they are the same connection.
From the perspective of system interfaces, you cannot create that situation because the system will not allow you to bind an address / port pair that is already in use to any socket (a stronger constraint than is strictly required). This means that one machine cannot use the same port simultaneously for both a client socket and a server socket, even for different remote endpoints.
You can, however, have any number of simultaneous TCP connections that each differ from all the others in at least one of those parameters. In particular, you can have any number of connections between the same two machines, with the same port on one side, and different ports on the other. This is extremely common, in fact, as web browsers often open multiple simultaneous connections to a web server to download multiple resources concurrently. All of those connections have the same server address, server port, and client address, but different client port.
If you want to have multiple simultaneous connections that are associated with one another in some way that goes beyond IP addresses, then you'll need to develop a protocol for it that involves multiple ports at at least one end. If the machines make reciprocal connections, with A connecting to B and then B connecting, separately, to A, then you'll need different ports on both sides. The port numbers to use might be fixed by the protocol or negotiated in some way, at your discretion, but the specifics described in the question are not an option.
I am trying to integrate multipath tcp (https://www.multipath-tcp.org/) into a project, and I would like to modify the source code in order to manually choose the outgoing socket port for a new mptcp subflow so that I can identify the packets going in and out within my application.
The address is created with:
inet_create(sock_net(meta_sk), &sock, IPPROTO_TCP, 1);
and bound:
sock.ops->bind(&sock, (struct sockaddr *)&loc_in, sizeof(struct sockaddr_in));
and then connected:
sock.ops->connect(&sock, (struct sockaddr *)&rem_in,
sizeof(struct sockaddr_in), O_NONBLOCK);
My question is this: How can I manually set the outgoing port of a socket at kernel level, and/or where is the port set in this sequence of calls, so I can modify it?
You would set the outgoing port in the loc_in structure that you use to bind() the socket to a local adapter/interface, eg:
struct sockaddr_in loc_in;
loc_in.sin_family = AF_INET;
// desired port...
loc_in.sin_port = htons(...);
// IP of desired adapter to connect() from...
loc_in.sin_addr.s_addr = inet_addr("...");
My goal is making a TCP/IP connection in a sender <-> server -> receiver fashion.
I have a server, sender and receiver. The server initial part looks like:
int main(int argc, char *argv[]){
int welcomeSocket, senderSocket;
char buffer[1024];
struct sockaddr_in serverAddr;
struct sockaddr_storage serverStorage;
socklen_t addr_size;
if (2 != argc) {
fprintf(stderr, "Usage: %s <port>\n", argv[0]);
exit(1);
}
/*---- Create the socket. The three arguments are: ----*/
/* 1) Internet domain 2) Stream socket 3) Default protocol (TCP in this case) */
welcomeSocket = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
/*---- Configure settings of the server address struct ----*/
/* Address family = Internet */
serverAddr.sin_family = AF_INET;
/* Set port number, using htons function to use proper byte order */
serverAddr.sin_port = htons(atoi(argv[1]));
/* Set IP address to localhost */
serverAddr.sin_addr.s_addr = htonl(INADDR_ANY);
//int len=20;
//char sbuffer[len];
//inet_ntop(AF_INET, &(serverAddr.sin_addr), sbuffer, len);
//printf("address:%s\n",sbuffer);
/* Set all bits of the padding field to 0 */
memset(serverAddr.sin_zero, '\0', sizeof serverAddr.sin_zero);
/*---- Bind the address struct to the socket ----*/
bind(welcomeSocket, (struct sockaddr *) &serverAddr, sizeof(serverAddr));
/*---- Listen on the socket, with 5 max connection requests queued ----*/
if(listen(welcomeSocket,5)==0)
printf("Listening\n");
else
printf("Error\n");
/*---- Accept call creates a new socket for the incoming connection ----*/
addr_size = sizeof serverStorage;
senderSocket = accept(welcomeSocket, (struct sockaddr *) &serverStorage, &addr_size);
//~~~~Some more code~~~
}
however this sets up the server ip address as 0.0.0.0. I have no clue whether this is correct or not, but all until now I had manually set up the ip address as 127.0.1.1 which is what I get when I type hostname -i in the terminal.
So, currently I am having my sender connect to the same IP address, but since I am working all files in the same computer I don't know if it will work across other computers in the network with this weird ip address 0.0.0.0. Can someone clarify (and maybe help fix) this small issue to me? I have tried reading other solutions that try to explain the difference between 0.0.0.0 and 127.0.0.1 but I couldn't find anything related to the performance on the connection/communication between server and sender in a TCP/IP connection.
And then the IPv4 address is listed as 129.15.78.12 in my system settings, but then again, not sure which one should be used for the server or receiver.
The difference is that by using INADDR_ANY you bind the service to all interfaces, as explained in
http://man7.org/linux/man-pages/man7/ip.7.html
and
understanding INADDR_ANY for socket programming - c
Other than that, if you keep this config any computer trying to reach your server will connect if it uses a valid external IP address, like the one you mention you see in the system settings. Hope this clarifies your question.
I'm trying to connect to a server I'm running in C on a computer on a wireless ad-hoc network. The problem is when I'm to connect from another computer on the network using telnet it doesn't work. I can ping the IP address (192.168.0.1) but using:
telnet 192.168.0.1 8889
results in the error "Connection Refused" (The server is listening on port 8889 which I've verified).
I've investigated further and found that setting up my ad-hoc network using network manager solves this problem. Currently I'm setting up my ad-hoc network via terminal and I would like to keep it this way. The ad-hoc network is being setup with the following commands:
sudo service network-manager stop
sudo iwconfig wlan0 mode ad-hoc essid 'rgd' channel AUTO key OFF
Where wlan0 is my wireless device.
I'm not sure why this error is occurring. Can anybody help me with this?
Edit: #Huygens netstat -tlpen displays the following
Proto Recv-Q Send-Q Local Address Foreign Address State User Inode PID/Program name
tcp 0 0 0.0.0.0:48727 0.0.0.0:* LISTEN 1000 302920 13098/socket
tcp 0 0 127.0.0.1:631 0.0.0.0:* LISTEN 0 273220 933/cupsd
tcp 0 0 0.0.0.0:54880 0.0.0.0:* LISTEN 1000 304089 13148/socket
tcp 0 0 127.0.1.1:53 0.0.0.0:* LISTEN 0 12886 1606/dnsmasq
tcp6 0 0 ::1:631 :::* LISTEN 0 273219 933/cupsd
tcp6 0 0 ::1:54822 :::* LISTEN 1000 46736 3297/java
I don't see my server here for some reason :(
I've added the server code I'm running here for further reference:
#include<stdio.h>
#include<string.h> //strlen
#include<sys/socket.h>
#include<arpa/inet.h> //inet_addr
#include<unistd.h> //write
int main(int argc , char *argv[])
{
int socket_desc , new_socket , c;
struct sockaddr_in server , client;
char *message;
//Create socket
socket_desc = socket(AF_INET , SOCK_STREAM , 0);
if (socket_desc == -1)
{
printf("Could not create socket");
}
//Prepare the sockaddr_in structure
server.sin_family = AF_INET;
server.sin_addr.s_addr = INADDR_ANY;
server.sin_port = htons( 8889 );
//Bind
if( bind(socket_desc,(struct sockaddr *)&server , sizeof(server)) < 0)
{
puts("bind failed");
return 1;
}
puts("bind done");
//Listen
listen(socket_desc , 3);
//Accept and incoming connection
puts("Waiting for incoming connections...");
c = sizeof(struct sockaddr_in);
new_socket = accept(socket_desc, (struct sockaddr *)&client, (socklen_t*)&c);
if (new_socket<0)
{
perror("accept failed");
return 1;
}
puts("Connection accepted");
//Reply to the client
message = "Hello Client , I have received your connection. But I have to go now, bye\n";
write(new_socket , message , strlen(message));
return 0;
}
You have to do things in the proper order I think.
First you should set-up your network interfaces. Once done, you should check that you can ping your network interfaces and that if you have a firewall that it would allow incoming connection on port 8889. To see of you have a firewall activated: iptables -L if there are any rule, then you have a firewall. In addition, make sure that each network interface has the expected IP address: ifconfig.
For testing purpose and if you are on a safe network, you can temporarily disable the firewall: iptables -F (precede it by sudo if you require super user privilege).
Then, you can start your server. Check that it is up and running (via ps wux) and check that it is in listening mode via netstat -tlpen.
Now try to telnet to it via telnet 192.168.0.1 8889.
PS: Of course your ad-hoc network should be on a different subnet than your local network. So if you are using both your ethernet (cable) network and an ad-hoc wifi network, each should be on a different subnet: e.g. 192.168.0.1/24 and 192.168.1.1/24.
I'm having trouble figuring this out - I'm working with sockets in C using this guide - http://binarii.com/files/papers/c_sockets.txt
I'm trying to automatically get my ip and port using:
server.sin_port = 0; /* bind() will choose a random port*/
server.sin_addr.s_addr = INADDR_ANY; /* puts server's IP automatically */
...
...
bind(int fd, struct sockaddr *my_addr,int addrlen); // Bind function
After a successful bind, how do I find out what IP and Port I'm actually assigned?
If it's a server socket, you should call listen() on your socket, and then getsockname() to find the port number on which it is listening:
struct sockaddr_in sin;
socklen_t len = sizeof(sin);
if (getsockname(sock, (struct sockaddr *)&sin, &len) == -1)
perror("getsockname");
else
printf("port number %d\n", ntohs(sin.sin_port));
As for the IP address, if you use INADDR_ANY then the server socket can accept connections to any of the machine's IP addresses and the server socket itself does not have a specific IP address. For example if your machine has two IP addresses then you might get two incoming connections on this server socket, each with a different local IP address. You can use getsockname() on the socket for a specific connection (which you get from accept()) in order to find out which local IP address is being used on that connection.
The comment in your code is wrong. INADDR_ANY doesn't put server's IP automatically'. It essentially puts 0.0.0.0, for the reasons explained in mark4o's answer.