I have a server:
./server portNumber
I have a client:
./client serverIpAddress
I have only server Ip Address in the client. I want to connect to Server socket. But every time Server Port Number is different. How Can I connect to server socket with BSD Socket in the client? Is it impossible? Do I have to know server port number?
Simple Usage BSD Socket:
int socket_desc;
struct sockaddr_in server;
//Create socket
socket_desc = socket(AF_INET , SOCK_STREAM , 0);
if (socket_desc == -1)
{
printf("Could not create socket");
}
server.sin_addr.s_addr = inet_addr(ipAddress);
server.sin_family = AF_INET;
server.sin_port = htons( ThisIsServerPortNumber );
//Connect to remote server
if (connect(socket_desc , (struct sockaddr *)&server , sizeof(server)) < 0)
{
puts("connect error");
return 1;
}