473,396 Members | 1,766 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

socket program help

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:
Expand|Select|Wrap|Line Numbers
  1. #include <unistd.h> 
  2. #include <stdio.h>
  3. #include <sys/types.h>
  4. #include <sys/socket.h>
  5. #include <stdlib.h>
  6. #include <unistd.h>
  7. #include <errno.h>
  8. #include <string.h>
  9. #include <netinet/in.h>
  10. #include <arpa/inet.h>
  11. #include <sys/wait.h>
  12. #include <signal.h>
  13. #include <ctype.h>
  14. #include <time.h>
  15. #define SERVERPORT 111              /* port used for the connection */  
  16. #define QUEUE 5                       /* max # of queued connects */
  17.  
  18. int main(void)
  19. {
  20.     fd_set master;   // master file descriptor list
  21.     fd_set read_fds; // temp file descriptor list for select()
  22.     int fdmax;        // maximum file descriptor number
  23.     int i , j;
  24.     int nbytes;
  25.      int s_listen,d_newconn;  
  26.     int yes=1;
  27.     struct sockaddr_in serveraddr;    /* server address */
  28.     struct sockaddr_in clientaddr;    /* client address */
  29.     struct sigaction sig;
  30.     socklen_t sin_size;               /* Socket address length type */
  31.     string buffer[256];
  32.  
  33.     FD_ZERO(&master);    // clear the master and temp sets
  34.     FD_ZERO(&read_fds);    
  35.  
  36.  
  37.     if ((s_listen = socket(AF_INET, SOCK_STREAM, 0)) == -1) {    /* create listen socket */
  38.         perror("socket");
  39.         exit(1);
  40.     }
  41.  
  42.     /* set the options for the socket to allow reuse of a local port/address combination. */
  43.  
  44.     if (setsockopt(s_listen,SOL_SOCKET,SO_REUSEADDR,&yes,sizeof(int)) == -1) {
  45.         perror("setsockopt");
  46.         exit(1);
  47.     }
  48.  
  49.       memset(&(serveraddr.sin_zero), '\0', 8);  /* clear our address */
  50.       serveraddr.sin_family = AF_INET;          /* Address Family */
  51.       serveraddr.sin_port = htons(SERVERPORT);  /* Port number */
  52.       /* a wild IP number to allow the system to pick the route to the remote service */
  53.       serveraddr.sin_addr.s_addr = INADDR_ANY;  
  54.  
  55.       /* bind address to socket */
  56.       if (bind(s_listen, (struct sockaddr *)&serveraddr, sizeof(struct sockaddr))  == -1) {
  57.          perror("bind");
  58.          exit(1);
  59.       }
  60.  
  61.       /*listen*/ 
  62.       if (listen(s_listen, QUEUE) == -1) {
  63.          perror("listen");
  64.          exit(1);
  65.       }
  66.  
  67. // add the listener to the master set
  68.     FD_SET(s_listen, &master);
  69.  
  70.     // keep track of the biggest file descriptor
  71.     fdmax = s_listen; // so far, it's this one
  72.  
  73.     // main loop
  74.     for(;;) {
  75.         read_fds = master; // copy it
  76.         if (select(fdmax+1, &read_fds, NULL, NULL, NULL) == -1) {
  77.             perror("select");
  78.             exit(1);
  79.         }
  80.  
  81.         // run through the existing connections looking for data to read
  82.         for(i = 0; i <= fdmax; i++) {
  83.             if (FD_ISSET(i, &read_fds)) { // we got one!!
  84.                 if (i == s_listen) {
  85.                     // handle new connections
  86.                     sin_size = sizeof(clientaddr);
  87.                     if ((d_newconn = accept(s_listen, (struct sockaddr *)&clientaddr,
  88.                                                                &sin_size)) == -1) { 
  89.                         perror("accept");
  90.                     } else {
  91.                         FD_SET(d_newconn, &master); // add to master set
  92.                         if (d_newconn > fdmax) {    // keep track of the maximum
  93.                             fdmax = d_newconn;
  94.                         }
  95.                         printf("EDMTS SERVER: new connection from %s on "
  96.                             "socket %d\n", inet_ntoa(clientaddr.sin_addr), d_newconn);
  97.                     }
  98.                 } else {
  99.                     // handle data from a client
  100.                     if ((nbytes = recv(i, buffer, sizeof(buffer), 0)) <= 0) {
  101.                         // got error or connection closed by client
  102.                         if (nbytes == 0) {
  103.                             // connection closed
  104.                             printf("SERVER: socket %d hung up\n", i);
  105.                         } else {
  106.                             perror("recv");
  107.                         }
  108.                         close(i); // bye!
  109.                         FD_CLR(i, &master); // remove from master set
  110.                     } else {
  111. /*
  112.                     {{{{{{{{{{{{{{{{{{{{{{ NOT COMPLETED }}}}}}}}}}}}}}}}}
  113.                     {{{{HERE I SHOULD COMPARE THE CLIENT MESSAGE PASSOWRD              IF IT'S THE SAME }}}}}}}}}}    */
  114.                     }
  115.                 } // it's SO UGLY!
  116.             }
  117.         }
  118.     }
  119.  
  120.     return 0;
  121. }
  122.  
Apr 2 '07 #1
0 1807

Sign in to post your reply or Sign up for a free account.

Similar topics

0
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...
11
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...
4
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...
4
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...
2
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....
2
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...
1
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...
6
Sagittarius
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...
0
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...
0
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...
0
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...
0
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
0
BarryA
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...
1
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...
0
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...
0
marktang
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,...
0
Oralloy
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,...
0
tracyyun
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...
0
agi2029
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,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.