Hi,
I'm writing a client-server socket program. the client will be an instance of the well-known telnet application. i want to implement a simple authentication between the server and the client.
- the client should send this message (after the connection established): my password "anypassword"
and the server will check, if it's not the same password that's hardcoded in the server program, the server close connection.
so the client will be like that:
1.telnet server
2. got connected
3. provide password, if it's not coreect server close connection
here is my server application: -
#include <unistd.h>
-
#include <stdio.h>
-
#include <sys/types.h>
-
#include <sys/socket.h>
-
#include <stdlib.h>
-
#include <unistd.h>
-
#include <errno.h>
-
#include <string.h>
-
#include <netinet/in.h>
-
#include <arpa/inet.h>
-
#include <sys/wait.h>
-
#include <signal.h>
-
#include <ctype.h>
-
#include <time.h>
-
#define SERVERPORT 111 /* port used for the connection */
-
#define QUEUE 5 /* max # of queued connects */
-
-
int main(void)
-
{
-
fd_set master; // master file descriptor list
-
fd_set read_fds; // temp file descriptor list for select()
-
int fdmax; // maximum file descriptor number
-
int i , j;
-
int nbytes;
-
int s_listen,d_newconn;
-
int yes=1;
-
struct sockaddr_in serveraddr; /* server address */
-
struct sockaddr_in clientaddr; /* client address */
-
struct sigaction sig;
-
socklen_t sin_size; /* Socket address length type */
-
string buffer[256];
-
-
FD_ZERO(&master); // clear the master and temp sets
-
FD_ZERO(&read_fds);
-
-
-
if ((s_listen = socket(AF_INET, SOCK_STREAM, 0)) == -1) { /* create listen socket */
-
perror("socket");
-
exit(1);
-
}
-
-
/* set the options for the socket to allow reuse of a local port/address combination. */
-
-
if (setsockopt(s_listen,SOL_SOCKET,SO_REUSEADDR,&yes,sizeof(int)) == -1) {
-
perror("setsockopt");
-
exit(1);
-
}
-
-
memset(&(serveraddr.sin_zero), '\0', 8); /* clear our address */
-
serveraddr.sin_family = AF_INET; /* Address Family */
-
serveraddr.sin_port = htons(SERVERPORT); /* Port number */
-
/* a wild IP number to allow the system to pick the route to the remote service */
-
serveraddr.sin_addr.s_addr = INADDR_ANY;
-
-
/* bind address to socket */
-
if (bind(s_listen, (struct sockaddr *)&serveraddr, sizeof(struct sockaddr)) == -1) {
-
perror("bind");
-
exit(1);
-
}
-
-
/*listen*/
-
if (listen(s_listen, QUEUE) == -1) {
-
perror("listen");
-
exit(1);
-
}
-
-
// add the listener to the master set
-
FD_SET(s_listen, &master);
-
-
// keep track of the biggest file descriptor
-
fdmax = s_listen; // so far, it's this one
-
-
// main loop
-
for(;;) {
-
read_fds = master; // copy it
-
if (select(fdmax+1, &read_fds, NULL, NULL, NULL) == -1) {
-
perror("select");
-
exit(1);
-
}
-
-
// run through the existing connections looking for data to read
-
for(i = 0; i <= fdmax; i++) {
-
if (FD_ISSET(i, &read_fds)) { // we got one!!
-
if (i == s_listen) {
-
// handle new connections
-
sin_size = sizeof(clientaddr);
-
if ((d_newconn = accept(s_listen, (struct sockaddr *)&clientaddr,
-
&sin_size)) == -1) {
-
perror("accept");
-
} else {
-
FD_SET(d_newconn, &master); // add to master set
-
if (d_newconn > fdmax) { // keep track of the maximum
-
fdmax = d_newconn;
-
}
-
printf("EDMTS SERVER: new connection from %s on "
-
"socket %d\n", inet_ntoa(clientaddr.sin_addr), d_newconn);
-
}
-
} else {
-
// handle data from a client
-
if ((nbytes = recv(i, buffer, sizeof(buffer), 0)) <= 0) {
-
// got error or connection closed by client
-
if (nbytes == 0) {
-
// connection closed
-
printf("SERVER: socket %d hung up\n", i);
-
} else {
-
perror("recv");
-
}
-
close(i); // bye!
-
FD_CLR(i, &master); // remove from master set
-
} else {
-
/*
-
{{{{{{{{{{{{{{{{{{{{{{ NOT COMPLETED }}}}}}}}}}}}}}}}}
-
{{{{HERE I SHOULD COMPARE THE CLIENT MESSAGE PASSOWRD IF IT'S THE SAME }}}}}}}}}} */
-
}
-
} // it's SO UGLY!
-
}
-
}
-
}
-
-
return 0;
-
}
-
0 1807 Sign in to post your reply or Sign up for a free account.
Similar topics
by: anuradha.k.r |
last post by:
hi,
i have written a simple socket program in python to connect to a
windows machine.On the server side i am running a windows socket
program which works perfectly fine,my server program waites at...
|
by: anuradha.k.r |
last post by:
hi,
i am writing a socket program in python,both client side and server
side.I've written the client side which is working perfectly
fine(checked it against server program written in C).but as for...
|
by: 0k |
last post by:
Hi everyone, I am trying to write a small app that sends multicast udp
packets using a socket object.
I have more than one NIC on my PC and the following code works OK only if I
disable all the...
|
by: Chris Tanger |
last post by:
Context:
C#
System.Net.Sockets
Socket created with constructor prarmeters Internetwork, Stream and TCP
everything else is left at the default parameters and options except linger
may be changed...
|
by: banduraj |
last post by:
I am working on starting and managing TCP connections manually. I build
the IP headers and TCP packets manually and send them on my own. The
problems I run into seems to be related to the Sockets....
|
by: Vitali Gontsharuk |
last post by:
Hi!
I have a problem programming a simple client-server game, which is
called pingpong ;-)
The final program will first be started as a server (nr. 2) and then as
a client. The client then...
|
by: Mr. Beck |
last post by:
Hello,
Please Help.....
I have been working with some tcp/ip socket communication within a C#
program recently. Basicly, I have a program (myProblemProgram) that has a
socket connected to...
|
by: Sagittarius |
last post by:
I will first try to describe my problem in words.
I have a simple program, written in C++, that needs to send a single bytearray via a UDP socket to a microprocessor, which returns an answer, also...
|
by: =?Utf-8?B?QWxwZXIgQUtDQVlPWg==?= |
last post by:
Hello,
First of all I wish you a good day.
My help request is about .NET asynchrounus socket communication. I have
developed Server-Client Windows Forms .NET applications in VC++ .NET v2003. I...
|
by: kaps |
last post by:
Hi all,
After reading for a while I have successfully prepared a server side listening and accepting multiple connections, and a client connecting to server.
I need help on loop of send/receive...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
| |