Sponsored Content
Top Forums Programming Need help about read() and write() on TCP/IP Post 302469543 by lixiao1212 on Saturday 6th of November 2010 10:27:09 PM
Old 11-06-2010
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!!
Code:
#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?

Last edited by lixiao1212; 11-07-2010 at 03:20 PM.. Reason: Please use code tags
 

10 More Discussions You Might Find Interesting

1. Programming

popening for read and write

How can 'popen()' be used for reading and writing to opening pipe? If i try 'popen("prog", "rw")' and then put and get chars - it does not work. What is wrong? (1 Reply)
Discussion started by: szzz
1 Replies

2. Shell Programming and Scripting

read and write from a file

I have tried to show the file name whose size is greater than 200 byte in current directory. Please help me. ls -l | tr -s " " " " | cut -f 5,9 -d " " >out.txt #set -a x `cat out.txt` i=0 `cat out.txt` | while do read x echo $x #re=200 j=0 if }" < "200" ] then echo $j j=`expr $j... (2 Replies)
Discussion started by: rinku
2 Replies

3. Shell Programming and Scripting

Find all files with group read OR group write OR user write permission

I need to find all the files that have group Read or Write permission or files that have user write permission. This is what I have so far: find . -exec ls -l {} \; | awk '/-...rw..w./ {print $1 " " $3 " " $4 " " $9}' It shows me all files where group read = true, group write = true... (5 Replies)
Discussion started by: shunter63
5 Replies

4. Programming

read/write files

Hi all, I have a problem with some read/write functions. I have a .bin file which contains a lot of structures as follows: struct alumno { char id; char apellido1; char apellido2; char nombre; float nota1p; float nota2p; float notamedia; char photofilename; }; What I have... (3 Replies)
Discussion started by: Attenea
3 Replies

5. IP Networking

read/write,write/write lock with smbclient fails

Hi, We have smb client running on two of the linux boxes and smb server on another linux system. During a backup operation which uses smb, read of a file was allowed while write to the same file was going on.Also simultaneous writes to the same file were allowed.Following are the settings in the... (1 Reply)
Discussion started by: swatidas11
1 Replies

6. IP Networking

write() / read() syntax

hi am newbie to unix and socket programing I am trying to figuring out syntax for read and write to send data from server to client and client can read it I have to send two integers write(newsockfd,buffer,"%d %d",x,y,0) writing from client where x and y are two integers.. ... (7 Replies)
Discussion started by: karthik1238
7 Replies

7. Shell Programming and Scripting

Read and write to tcp socket

Hello all, I have a requirement to read and write to a tcp socket from an HP-UX shell script. I see a /dev/tcp character device on my servers: crw-rw-rw- 1 root root 72 0x00004f Mar 28 18:37 /dev/tcp So I believe this is what I should use. The problem is that all the... (2 Replies)
Discussion started by: lupin..the..3rd
2 Replies

8. Shell Programming and Scripting

File Read and Write

I have got a file in following format: AAAAAAA BBBBBBBB CCCCCCC DDDDDDD I am trying to read this file and out put it in following format: AAAAAAA,BBBBBBB,CCCCCCC,DDDDDD Preferred method is shell or Perl. Any help appreciated. (11 Replies)
Discussion started by: Araoki
11 Replies

9. Shell Programming and Scripting

Read and write in the file

Hello Guys, How all are doing? I have an issue in Unix and want help from all of you I have a file in UNIX which it read by line by line , If at the end of line '0' is written the it should fetch that line into another file and change '0' to '1' and If at the end of line '1' is written then it... (10 Replies)
Discussion started by: adisky123
10 Replies

10. Programming

FIFO write and read

Can someone help me to write this program in C in QNX? Using the FIFO queues write a simple communication system consisting of programs write and read. The program write the parameters given strings enclosed in single quotes. These strings are written to the FIFO file. Reads the program read... (1 Reply)
Discussion started by: ebasse2
1 Replies
TCP_TABLE(5)							File Formats Manual						      TCP_TABLE(5)

NAME
tcp_table - Postfix client/server table lookup protocol SYNOPSIS
postmap -q "string" tcp:host:port postmap -q - tcp:host:port <inputfile DESCRIPTION
The Postfix mail system uses optional tables for address rewriting or mail routing. These tables are usually in dbm or db format. Alterna- tively, table lookups can be directed to a TCP server. To find out what types of lookup tables your Postfix system supports use the "postconf -m" command. To test lookup tables, use the "postmap -q" command as described in the SYNOPSIS above. PROTOCOL DESCRIPTION
The TCP map class implements a very simple protocol: the client sends a request, and the server sends one reply. Requests and replies are sent as one line of ASCII text, terminated by the ASCII newline character. Request and reply parameters (see below) are separated by white- space. Send and receive operations must complete in 100 seconds. REQUEST FORMAT
Each request specifies a command, a lookup key, and possibly a lookup result. get SPACE key NEWLINE Look up data under the specified key. put SPACE key SPACE value NEWLINE This request is currently not implemented. REPLY FORMAT
Each reply specifies a status code and text. Replies must be no longer than 4096 characters including the newline terminator. 500 SPACE text NEWLINE In case of a lookup request, the requested data does not exist. In case of an update request, the request was rejected. The text describes the nature of the problem. 400 SPACE text NEWLINE This indicates an error condition. The text describes the nature of the problem. The client should retry the request later. 200 SPACE text NEWLINE The request was successful. In the case of a lookup request, the text contains an encoded version of the requested data. ENCODING
In request and reply parameters, the character %, each non-printing character, and each whitespace character must be replaced by %XX, where XX is the corresponding ASCII hexadecimal character value. The hexadecimal codes can be specified in any case (upper, lower, mixed). The Postfix client always encodes a request. The server may omit the encoding as long as the reply is guaranteed to not contain the % or NEWLINE character. SECURITY
Do not use TCP lookup tables for security critical purposes. The client-server connection is not protected and the server is not authenti- cated. BUGS
Only the lookup method is currently implemented. The client does not hang up when the connection is idle for a long time. SEE ALSO
postmap(1), Postfix lookup table manager regexp_table(5), format of regular expression tables pcre_table(5), format of PCRE tables cidr_table(5), format of CIDR tables README FILES
Use "postconf readme_directory" or "postconf html_directory" to locate this information. DATABASE_README, Postfix lookup table overview LICENSE
The Secure Mailer license must be distributed with this software. AUTHOR(S) Wietse Venema IBM T.J. Watson Research P.O. Box 704 Yorktown Heights, NY 10598, USA TCP_TABLE(5)
All times are GMT -4. The time now is 09:19 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy