Need help about read() and write() on TCP/IP

HI

I need to implement a client/server TCP application. the customer is the client and the bartender is the server.

When the customer enter the Bar, client connects to the server
Server should reply the client immediately. Other wise if the server is busy, it should send an update message every 10 seconds.

Depending on which message replied by the server:

10 seconds expired, no reply: client leave, that is disconnect
server is busy update: client reply --waiting or "leave and disconnect"
select beer: client reply -- light or black
select quantity: client reply liter or bottle, then disconnect

I have done the part before 'connection', but i don't know how to complement the function about read() and write(), so, please help me have a look at it, many thanks!!

#include <sys/wait.h>
#include <malloc.h>
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <errno.h>
#include <stdlib.h>
#include <arpa/inet.h>
int main(int argc, char *argv[])
{
    int   socket1,n;   
        int blond=0 , brun=0;//string buf;
    char buffer[256];
    if(argc<2) {
        printf ("usage socket_clt \n");
        exit(0);
    }
 
    printf("client1 is coming\n");
        struct sockaddr_in local;
        struct sockaddr_in from;
        int fromlen =sizeof(from);
    bzero(&from, sizeof(from));
        from.sin_family=AF_INET;
        from.sin_port=htons(5000); //lisening 5000
        from.sin_addr.s_addr=INADDR_ANY; //address of local computer
    /*creat a socket*/   
    if (( socket1=socket(AF_INET, SOCK_STREAM,0))<0)
         {
        printf("socket error ourverture\n");exit(0);
    }
 
    if (connect(socket1, (struct sockaddr*)&from, sizeof(from))<0)
         {
        printf("socket error connection\n");
        exit(0);
    }
 
    bzero(buffer,256);
      fgets(buffer,255,stdin);
      n = write(socket1,buffer,strlen(buffer));
      if (n < 0)
           error("ERROR writing to socket");
 
  bzero(buffer,256);
      n = read(socket1,buffer,255);
      if (n < 0)
           error("ERROR reading from socket");
      printf("%s\n",buffer);
      return 0;
 
}

I need to add a function of 'timeout' on read(),but i don't know how to do that.
thus how can i add a circle to implement many customers entering?

Is it a homework? In this case, post in the Homework forum...

You first need to define a protocol between your server and your client (on top of TCP), so that both understand the command "server is busy update", "select beer", "light/black", "select quantity", "liter/bottle"

On the client side:

  • The client connects to the server.
  • The client reads the answer from the server. Note that you will have to implement a timeout on the read().
  • The client sends back answer to the server as appropriate.

On the server side:

  • the server accepts the connection
  • the server sends reply to the client
  • the server reads reply from the client
    The implementation of the "server busy update" is tricky: you'll have to fork (or create a new thread) upon each connection. Only one client can be served at a time; the other children/threads should take care of the "busy update".

HTH,
Lo�c

thanks Loic but i just need to finish the part of client, so could you tell me how can i implement it? thanks again!!

How does the protocol between server and client look like?

TCP

"what format is the data in?" "file"

You can send anything you want over TCP. It's up to you to decide what the protocol is.

i don't understand, i have decided to use the protocol on TCP.
data format? what's your mean?

Just like you can save anything you want in a file, you can send anything you want over a socket. You must decide what bytes of data you will send to mean what. Are you sending lines of text? Are you sending binary structures? Something else? What are you doing?

Basically. the client and server exchange a stream of bytes. That is both send a bunch of bytes, and receive a bunch of bytes. Client and server needs to agree how to decode and interpret this bunch of bytes; this is what we call protocol between the client and the server.

For instance, when you're browsing this very site, a protocol based on TCP called HTTP is used between your web browser and the (apache) server.

So now the question is: how will the client understand the reply "busy update", "select beer", "select quantity" ? How will the client send to the server "light", "black" or "liter","bottle" ? What convention are you using? strings ? something else?

May I recommend this book UNIX Network Programming

Cheers,
Lo�c

I finally understood what you guys mean, actually,i want to send lines of text for asking the beer. i have checked something about socket, TCP, but still have no idea for my program, please give me a hand.

thank you
Xiao

---------- Post updated at 04:36 PM ---------- Previous update was at 04:33 PM ----------

yes, that is make me understand what you mean, i'm appreciate for your patience, i need to order beer by lines of text, so how can i do next step?

Dear Lixiao,

start studying and writing your 'hello world' client/server, see for instance Programming IP Sockets on Linux, Part One . Then apply your gained knowledges to solve your particular problem.

You must enter the (socket programming) Path yourself :slight_smile:

1 Like