Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Q&A

Welcome to Software Development on Codidact!

Will you help us build our independent community of developers helping developers? We're small and trying to grow. We welcome questions about all aspects of software development, from design to code to QA and more. Got questions? Got answers? Got code you'd like someone to review? Please join us.

Post History

50%
+2 −2
Q&A How can you modify your server code to make it accept any port numbers instead of just one which is the one you predefined?

Is there any way where I can modify the original server code so that it accepts any port number instead of the predefined one (i.e., port number 8989)? Port number will be entered in the command li...

0 answers  ·  posted 3y ago by hamburgersarecool‭  ·  edited 3y ago by Alexei‭

Question c sockets port winsock2
#3: Post edited by user avatar Alexei‭ · 2021-12-08T12:38:49Z (almost 3 years ago)
fixed code + added relevant tags
  • Is there anyway where I can modify the original server code so that it accepts any port number instead of the predefined one (i.e., port number 8989). Port number will be entered in the command line when testing the Server1.exe as follows: -Networking 1>start server 8989
  • (also the main function parameters holds the port the command line arguments)
  • ````#include<io.h>
  • #include<stdio.h>
  • #include<winsock2.h>
  • #define MY_PORT 8989
  • #define MAXBUF 256
  • int main(int argc , char *argv[])
  • {
  • WSADATA wsa;
  • SOCKET sockfd , clientfd;
  • struct sockaddr_in self;
  • char buffer[MAXBUF];
  • printf("\nInitialising Winsock...");
  • if (WSAStartup(MAKEWORD(2,2),&wsa) != 0)
  • {
  • printf("Failed. Error Code : %d",WSAGetLastError());
  • return 1;
  • }
  • printf("Initialised.\n");
  • /*---create streaming socket---*/
  • if ( (sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0 )
  • {
  • perror("Socket");
  • exit(errno);
  • }
  • printf("Socket created.\n");
  • /*---initialize address/port structure---*/
  • /* bzero(&self, sizeof(self));*/
  • self.sin_family = AF_INET;
  • self.sin_port = htons(MY_PORT); // Host to Network Short (16-bit)
  • self.sin_addr.s_addr = INADDR_ANY;
  • /*---assign a port number to the socket---*/
  • if ( bind(sockfd, (struct sockaddr*)&self, sizeof(self)) != 0 )
  • {
  • perror("socket--bind");
  • exit(errno);
  • }
  • puts("Bind done");
  • /*---make it a "listening socket"---*/
  • if ( listen(sockfd, 20) != 0 )
  • {
  • perror("socket--listen");
  • exit(errno);
  • }
  • puts("Waiting for incoming connections...");
  • /*---forever... ---*/
  • while (1)
  • { struct sockaddr_in client_addr;
  • int addrlen=sizeof(client_addr);
  • /*---accept a connection (creating a data pipe)---*/
  • clientfd = accept(sockfd, (struct sockaddr*)&client_addr, &addrlen);
  • send(clientfd, buffer, recv(clientfd, buffer, MAXBUF, 0), 0);
  • /*---close connection---*/
  • close(clientfd);
  • }
  • /*---clean up (should never get here!)---*/
  • close(sockfd);
  • WSACleanup();
  • return 0;
  • }
  • Is there any way where I can modify the original server code so that it accepts any port number instead of the predefined one (i.e., port number 8989)? Port number will be entered in the command line when testing the Server1.exe as follows:
  • `start server 8989`
  • (also the main function parameters holds the port the command line arguments)
  • ```c
  • #include<io.h>
  • #include<stdio.h>
  • #include<winsock2.h>
  • #define MY_PORT 8989
  • #define MAXBUF 256
  • int main(int argc , char *argv[])
  • {
  • WSADATA wsa;
  • SOCKET sockfd , clientfd;
  • struct sockaddr_in self;
  • char buffer[MAXBUF];
  • printf("\nInitialising Winsock...");
  • if (WSAStartup(MAKEWORD(2,2),&wsa) != 0)
  • {
  • printf("Failed. Error Code : %d",WSAGetLastError());
  • return 1;
  • }
  • printf("Initialised.\n");
  • /*---create streaming socket---*/
  • if ( (sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0 )
  • {
  • perror("Socket");
  • exit(errno);
  • }
  • printf("Socket created.\n");
  • /*---initialize address/port structure---*/
  • /* bzero(&self, sizeof(self));*/
  • self.sin_family = AF_INET;
  • self.sin_port = htons(MY_PORT); // Host to Network Short (16-bit)
  • self.sin_addr.s_addr = INADDR_ANY;
  • /*---assign a port number to the socket---*/
  • if ( bind(sockfd, (struct sockaddr*)&self, sizeof(self)) != 0 )
  • {
  • perror("socket--bind");
  • exit(errno);
  • }
  • puts("Bind done");
  • /*---make it a "listening socket"---*/
  • if ( listen(sockfd, 20) != 0 )
  • {
  • perror("socket--listen");
  • exit(errno);
  • }
  • puts("Waiting for incoming connections...");
  • /*---forever... ---*/
  • while (1)
  • { struct sockaddr_in client_addr;
  • int addrlen=sizeof(client_addr);
  • /*---accept a connection (creating a data pipe)---*/
  • clientfd = accept(sockfd, (struct sockaddr*)&client_addr, &addrlen);
  • send(clientfd, buffer, recv(clientfd, buffer, MAXBUF, 0), 0);
  • /*---close connection---*/
  • close(clientfd);
  • }
  • /*---clean up (should never get here!)---*/
  • close(sockfd);
  • WSACleanup();
  • return 0;
  • }
  • ```
#2: Post edited by user avatar hamburgersarecool‭ · 2021-12-08T10:34:58Z (almost 3 years ago)
  • Is there anyway where I can modify the original server code so that it accepts any port number instead of the predefined one (i.e., port number 8989). Port number will be entered in the command line when testing the Server1.exe as follows: -Networking 1>start server 8989
  • ````#include<io.h>
  • #include<stdio.h>
  • #include<winsock2.h>
  • #define MY_PORT 8989
  • #define MAXBUF 256
  • int main(int argc , char *argv[])
  • {
  • WSADATA wsa;
  • SOCKET sockfd , clientfd;
  • struct sockaddr_in self;
  • char buffer[MAXBUF];
  • printf("\nInitialising Winsock...");
  • if (WSAStartup(MAKEWORD(2,2),&wsa) != 0)
  • {
  • printf("Failed. Error Code : %d",WSAGetLastError());
  • return 1;
  • }
  • printf("Initialised.\n");
  • /*---create streaming socket---*/
  • if ( (sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0 )
  • {
  • perror("Socket");
  • exit(errno);
  • }
  • printf("Socket created.\n");
  • /*---initialize address/port structure---*/
  • /* bzero(&self, sizeof(self));*/
  • self.sin_family = AF_INET;
  • self.sin_port = htons(MY_PORT); // Host to Network Short (16-bit)
  • self.sin_addr.s_addr = INADDR_ANY;
  • /*---assign a port number to the socket---*/
  • if ( bind(sockfd, (struct sockaddr*)&self, sizeof(self)) != 0 )
  • {
  • perror("socket--bind");
  • exit(errno);
  • }
  • puts("Bind done");
  • /*---make it a "listening socket"---*/
  • if ( listen(sockfd, 20) != 0 )
  • {
  • perror("socket--listen");
  • exit(errno);
  • }
  • puts("Waiting for incoming connections...");
  • /*---forever... ---*/
  • while (1)
  • { struct sockaddr_in client_addr;
  • int addrlen=sizeof(client_addr);
  • /*---accept a connection (creating a data pipe)---*/
  • clientfd = accept(sockfd, (struct sockaddr*)&client_addr, &addrlen);
  • send(clientfd, buffer, recv(clientfd, buffer, MAXBUF, 0), 0);
  • /*---close connection---*/
  • close(clientfd);
  • }
  • /*---clean up (should never get here!)---*/
  • close(sockfd);
  • WSACleanup();
  • return 0;
  • }
  • Is there anyway where I can modify the original server code so that it accepts any port number instead of the predefined one (i.e., port number 8989). Port number will be entered in the command line when testing the Server1.exe as follows: -Networking 1>start server 8989
  • (also the main function parameters holds the port the command line arguments)
  • ````#include<io.h>
  • #include<stdio.h>
  • #include<winsock2.h>
  • #define MY_PORT 8989
  • #define MAXBUF 256
  • int main(int argc , char *argv[])
  • {
  • WSADATA wsa;
  • SOCKET sockfd , clientfd;
  • struct sockaddr_in self;
  • char buffer[MAXBUF];
  • printf("\nInitialising Winsock...");
  • if (WSAStartup(MAKEWORD(2,2),&wsa) != 0)
  • {
  • printf("Failed. Error Code : %d",WSAGetLastError());
  • return 1;
  • }
  • printf("Initialised.\n");
  • /*---create streaming socket---*/
  • if ( (sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0 )
  • {
  • perror("Socket");
  • exit(errno);
  • }
  • printf("Socket created.\n");
  • /*---initialize address/port structure---*/
  • /* bzero(&self, sizeof(self));*/
  • self.sin_family = AF_INET;
  • self.sin_port = htons(MY_PORT); // Host to Network Short (16-bit)
  • self.sin_addr.s_addr = INADDR_ANY;
  • /*---assign a port number to the socket---*/
  • if ( bind(sockfd, (struct sockaddr*)&self, sizeof(self)) != 0 )
  • {
  • perror("socket--bind");
  • exit(errno);
  • }
  • puts("Bind done");
  • /*---make it a "listening socket"---*/
  • if ( listen(sockfd, 20) != 0 )
  • {
  • perror("socket--listen");
  • exit(errno);
  • }
  • puts("Waiting for incoming connections...");
  • /*---forever... ---*/
  • while (1)
  • { struct sockaddr_in client_addr;
  • int addrlen=sizeof(client_addr);
  • /*---accept a connection (creating a data pipe)---*/
  • clientfd = accept(sockfd, (struct sockaddr*)&client_addr, &addrlen);
  • send(clientfd, buffer, recv(clientfd, buffer, MAXBUF, 0), 0);
  • /*---close connection---*/
  • close(clientfd);
  • }
  • /*---clean up (should never get here!)---*/
  • close(sockfd);
  • WSACleanup();
  • return 0;
  • }
#1: Initial revision by user avatar hamburgersarecool‭ · 2021-12-08T10:34:17Z (almost 3 years ago)
How can you modify your server code to make it accept any port numbers instead of just one which is the one you predefined?
Is there anyway where I can modify the original server code so that it accepts any port number instead of the predefined one (i.e., port number 8989). Port number will be entered in the command line when testing the Server1.exe as follows: -Networking 1>start server 8989
````#include<io.h>
#include<stdio.h>
#include<winsock2.h>

#define MY_PORT		8989
#define MAXBUF		256

int main(int argc , char *argv[])
{
    WSADATA wsa;
    SOCKET sockfd , clientfd;
    	struct sockaddr_in self;
	char buffer[MAXBUF];

    printf("\nInitialising Winsock...");
    if (WSAStartup(MAKEWORD(2,2),&wsa) != 0)
    {
        printf("Failed. Error Code : %d",WSAGetLastError());
        return 1;
    }

    printf("Initialised.\n");

	/*---create streaming socket---*/
    if ( (sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0 )
	{
		perror("Socket");
		exit(errno);
	}

        printf("Socket created.\n");

	/*---initialize address/port structure---*/
	/* bzero(&self, sizeof(self));*/
	self.sin_family = AF_INET;
	self.sin_port = htons(MY_PORT);	  // Host to Network Short (16-bit)
	self.sin_addr.s_addr = INADDR_ANY;

	/*---assign a port number to the socket---*/
    if ( bind(sockfd, (struct sockaddr*)&self, sizeof(self)) != 0 )
	{
		perror("socket--bind");
		exit(errno);
	}

        puts("Bind done");

	/*---make it a "listening socket"---*/
	if ( listen(sockfd, 20) != 0 )
	{
		perror("socket--listen");
		exit(errno);
	}

        puts("Waiting for incoming connections...");

	/*---forever... ---*/
	while (1)
	{	struct sockaddr_in client_addr;
		int addrlen=sizeof(client_addr);

		/*---accept a connection (creating a data pipe)---*/
		clientfd = accept(sockfd, (struct sockaddr*)&client_addr, &addrlen);

		send(clientfd, buffer, recv(clientfd, buffer, MAXBUF, 0), 0);

		/*---close connection---*/
		close(clientfd);
	}

	/*---clean up (should never get here!)---*/
	close(sockfd);
        WSACleanup();
	return 0;
}