The question asked me to:
- 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 (Hint given: the main function parameters holds the port the command line arguments)
- modify the original server code so that it closes the connection and terminate if the client input message is “exit server”. Moreover the server should modify the client message to be in uppercase and it should print it accordingly.
I tried doing these 2 parts individually using normal C coding already but I'm unsure on how to combine it into the original server code/base code T.T
The base code/original server code:
////////////////////////// Server.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;
}
The code I created using normal c programming to address part 1) of the question:
#include<stdio.h>
int main (void){
int port_no;
printf("Enter a port number:\n");
scanf("%d", &port_no);
}
The code I created using normal c programming to address part 2) of the question:
#include<stdio.h>
#include<ctype.h>
#define MAX 100
void conToupper(char *aPtr);
int main (void){
int port_no; //changed
char exit[MAX];
char a[MAX] = "exit server";
printf("If you'd like to end, input 'exit server':");
scanf("%s", &exit);
for(int i=0; i<MAX; i++){
if (exit[i] == a[i]){
conToupper(a);
printf("%s\n",a);
break;
}
}
}
void conToupper(char *aPtr){
while(*aPtr != '\0'){
*aPtr = toupper(*aPtr);
++aPtr;
}
}
The problem I have is, right now how can I possibly combine these parts 1) & parts 2) in the base code provided? Please bear with me I just got into socket programming :") Also, I'm not so sure if I'm approaching this question in the right way..