Sponsored Content
Full Discussion: Tcp Ip Server
Top Forums Programming Tcp Ip Server Post 46969 by massimo_ratti on Friday 30th of January 2004 05:04:11 AM
Old 01-30-2004
Question Tcp Ip Server

i am programming a tcp_ip server which intends to listen permanently to a client . the client can disconnect and connect again and the server accept it(by this point it works).The problem is when the client lose connection without a disconnect command and my code can't get it and keeps waiting for some message to come with the read command considering that socket still working. i need it to exit from read command and close the socket in order to accept another one...
I thought to use a separate thread which consider the connection lost if it doesn't read anything from the client for a certain period of time...but it seems strange to me that there's not a prepared function in some tcp library that already does it.
ANY SUGGESTMENTS? THANKS A LOT
Massimo
Here's a piece of the code:

void die(char * message)
{

perror(message);
exit(1);

}

void copyData (int from, int to)
{

while ((len = read(from, buff, sizeof(buff))) > 0)
{
interpreta_messaggio(buff);

}
close(from);

}
void main()
{
struct sockaddr_in address;
int sock, conn, i;
size_t addrLength = sizeof(struct sockaddr_in);

if ((sock = socket(PF_INET,SOCK_STREAM,0)) < 0)
die("socket");

i=1;
setsockopt(sock,SOL_SOCKET,SO_REUSEADDR, &i,sizeof(i));

address.sin_family = AF_INET;
address.sin_port = htons(7000);
memset(&address.sin_addr, 0, sizeof(address.sin_addr));

if (bind(sock, (struct sockaddr *) &address, sizeof(address)))
die("bind");

if (listen(sock,5))
die("listen") ;

while (( new_socket = accept(sock, (struct sockaddr *) &address,&addrLength)) >=0)

{
copyData(new_socket,1);
}

if (new_socket < 0)
die("accept");

close(sock);
//return EXIT_SUCCESS;
}
Smilie
 

10 More Discussions You Might Find Interesting

1. IP Networking

Tcp Ip Send Receive Server Program

Requirements: A server program should read a file and send the message to the client . if the file is not there, then switch to the receive part of the same program and receive any messages from the socket. If no messages to receive then switch to send part of the program to... (2 Replies)
Discussion started by: Rajeshsu
2 Replies

2. UNIX for Dummies Questions & Answers

Tcp-server

I have true64 Unix running and there a scales in the sytem which connect thru a com-server to the network. they have their own IP-address and are communicating over port 8000. when I telnet to the com-servers and the print function of the scale is executed I can see the data coming. I need to know... (1 Reply)
Discussion started by: albinhess
1 Replies

3. Programming

How to check TCP server status

Please tell me according to C/C++ socket programming; how client can check whether server is running or not during TCP communication. (1 Reply)
Discussion started by: mansoorulhaq
1 Replies

4. Programming

tcp server using pthreads is not working...ne help plz?

Hi, I am new to using threads in C++ though I have been wkring on C++ for past 1.5 years...I want to write a TCP server that serves multiple client connections...to start off..i have been working on a simple tcp echo server trying to understand how threads work.... this is my server code: ... (5 Replies)
Discussion started by: deepti_v25
5 Replies

5. IP Networking

TCP server dies after few hours of inactivity

We have a NAS application which can be accessed by both HTTP and HTTPS connections. The issue we are facing is that the tcp server instance that initiates the HTTP access dies after a few hours of inactivity(the NAS application was kept idle for around 10 hours). However, the tcpserver that... (1 Reply)
Discussion started by: swatidas11
1 Replies

6. Programming

Problem with tcp server

Hello @ all, I hope you can give me some advice :b: I will be following code for a tcp server and doStuff () function, the clients treated. From some point, I have several identical clients (zombies, I think), the same records in the database write. Has anyone an explanation? What can I... (1 Reply)
Discussion started by: yumos
1 Replies

7. Programming

Concurrent TCP client/server

I made a program and now I need to make it concurrent. Can someone pls help me do this ? The code is this: #include <sys/types.h> #include <sys/socket.h> #include <sys/time.h> #include <netinet/in.h> #include <errno.h> #include <unistd.h> #include <stdio.h> #include <string.h>... (4 Replies)
Discussion started by: Johnny22
4 Replies

8. Solaris

TCP listner on Solaris server

Hi, I am having a solaris server. I want to start a dummy TCP listner on UNIX OS on a specific port can anyone please let me know the process. IP ADDRESS: 123.123.123.123 Port: 8010 (1 Reply)
Discussion started by: mayank2211
1 Replies

9. UNIX for Dummies Questions & Answers

Tcp connection to Linux server fails

I am trying to send json messages to a port on a linux server from a remote server running a .net program. I have one implementation running with successful incoming messages to port 1514. I tried to replicate the same thing but just to another port but cannot get it to work as I get the following... (3 Replies)
Discussion started by: unienewbie
3 Replies

10. Solaris

Too much TCP retransmitted and TCP duplicate on server Oracle Solaris 10

I have problem with oracle solaris 10 running on oracle sparc T4-2 server. Os information: 5.10 Generic_150400-03 sun4v sparc sun4v Output from tcpstat.d script TCP bytes: out outRetrans in inDup inUnorder 6833763 7300 98884 0... (2 Replies)
Discussion started by: insatiable1610
2 Replies
ivykis(3)						    ivykis programmer's manual							 ivykis(3)

NAME
iv_examples - ivykis examples EXAMPLE
ivykis is initialised by calling iv_init(3). This function is the first function to call when dealing with ivykis -- it has to be called before registering file descriptors or timers. The ivykis main event loop is started by calling iv_main(3). This function generally does not return, except when iv_quit(3) is called somewhere during execution of the program. An application asks ivykis to monitor a certain file descriptor by filling out a structure of type 'struct iv_fd' with a file descriptor number and a callback function, and calling the function iv_fd_register. The first example program waits for data from standard input, and writes a message to standard out whenever something is received: #include <stdio.h> #include <stdlib.h> #include <iv.h> struct iv_fd fd_stdin; static void callback(void *dummy) { char buf[1024]; int len; len = read(fd_stdin.fd, buf, sizeof(buf)); if (len <= 0) { if (len < 0) { if (errno == EAGAIN) return; perror("read"); } exit(1); } printf("read %d bytes of data from stdin ", len); } int main() { iv_init(); IV_FD_INIT(&fd_stdin); fd_stdin.fd = 0; fd_stdin.handler_in = callback; iv_fd_register(&fd_stdin); iv_main(); iv_deinit(); return 0; } The application is responsible for memory management of 'struct iv_fd's passed to ivykis. For example, it should not free memory that con- tains such structures that are still registered with ivykis (i.e. haven't had iv_fd_unregister called on them). iv_fd_register transparently sets the passed file descriptor to nonblocking mode, in anticipation of its future usage. File descriptor callbacks are called in a level-triggered fashion. Therefore, the way of dealing with fd_stdin in the example callback function is safe. In case there arrives data between read and detecting EAGAIN, ivykis will re-call the callback function after it returns. Also, if there are more than 1024 bytes waiting in the input buffer, ivykis will re-call the callback function until all data from stdin have been drained. EXAMPLE 2 The second example accepts connections on TCP port 6667, and waits on each of the connections for data. When data is received on any con- nection, a message is printed to standard out. #include <stdio.h> #include <stdlib.h> #include <iv.h> #include <netinet/in.h> struct connection { struct iv_fd fd; /* other per-connection data goes here */ }; struct listening_socket { struct iv_fd fd; /* other per-listening socket data goes here */ }; static void connection_handler(void *_conn) { struct connection *conn = (struct connection *)_conn; char buf[1024]; int len; len = read(conn->fd.fd, buf, sizeof(buf)); if (len <= 0) { if (len < 0 && errno == EAGAIN) return; iv_fd_unregister(&conn->fd); close(conn->fd.fd); free(conn); return; } printf("got %d bytes of data from %p ", len, conn); } static void listening_socket_handler(void *_sock) { struct listening_socket *sock = (struct listening_socket *)_sock; struct sockaddr_in addr; socklen_t addrlen; struct connection *conn; int fd; addrlen = sizeof(addr); fd = accept(sock->fd.fd, (struct sockaddr *)&addr, &addrlen); if (fd < 0) { if (errno == EAGAIN) return; perror("accept"); exit(1); } conn = malloc(sizeof(*conn)); if (conn == NULL) { fprintf(stderr, "listening_socket_handler: memory allocation error, dropping connection"); close(fd); return; } IV_FD_INIT(&conn->fd); conn->fd.fd = fd; conn->fd.cookie = (void *)conn; conn->fd.handler_in = connection_handler; iv_fd_register(&conn->fd); } int main() { struct listening_socket s; struct sockaddr_in addr; int fd; fd = socket(AF_INET, SOCK_STREAM, 0); if (fd < 0) { perror("socket"); exit(1); } addr.sin_family = AF_INET; addr.sin_addr.s_addr = htonl(INADDR_ANY); addr.sin_port = htons(6667); if (bind(fd, (struct sockaddr *)&addr, sizeof(addr)) < 0) { perror("bind"); exit(1); } if (listen(fd, 4) < 0) { perror("listen"); exit(1); } iv_init(); IV_FD_INIT(&s.fd); s.fd.fd = fd; s.fd.cookie = (void *)&s; s.fd.handler_in = listening_socket_handler; iv_fd_register(&s.fd); iv_main(); iv_deinit(); return 0; } As illustrated, it is possible to pass cookies into callback functions. This is useful for conveying information on which higher-level entity (such as 'connection' or 'listening socket') generated the event for which the callback was called. Note how it is possible to unregister and even free a 'struct iv_fd' in its own callback function. There is logic in ivykis to deal with this case. EXAMPLE 3 This example extends the previous example by a per-connection timer that disconnects the client after too long a period of inactivity. Lines not present in example 2 or different than in example 2 are indicated by '//XXXX' in the right-hand margin. #include <stdio.h> #include <stdlib.h> #include <iv.h> #include <netinet/in.h> #define CONNECTION_TIMEOUT(10) struct connection { struct iv_fd fd; struct iv_timer disconnect_timeout; //XXXX /* other per-connection data goes here */ }; struct listening_socket { struct iv_fd fd; /* other per-listening socket data goes here */ }; static void connection_handler(void *_conn) { struct connection *conn = (struct connection *)_conn; char buf[1024]; int len; len = read(conn->fd.fd, buf, sizeof(buf)); if (len <= 0) { if (len < 0 && errno == EAGAIN) return; iv_timer_unregister(&conn->disconnect_timeout); //XXXX iv_fd_unregister(&conn->fd); close(conn->fd.fd); free(conn); return; } printf("got %d bytes of data from %p ", len, conn); iv_timer_unregister(&conn->disconnect_timeout); //XXXX iv_validate_now(); //XXXX conn->disconnect_timeout.expires = iv_now; //XXXX conn->disconnect_timeout.expires.tv_sec += CONNECTION_TIMEOUT;//XXXX iv_timer_register(&conn->disconnect_timeout); //XXXX } static void disconnect_timeout_expired(void *_conn) //XXXX { //XXXX struct connection *conn = (struct connection *)_conn; //XXXX iv_fd_unregister(&conn->fd); //XXXX close(conn->fd.fd); //XXXX free(conn); //XXXX } //XXXX static void listening_socket_handler(void *_sock) { struct listening_socket *sock = (struct listening_socket *)_sock; struct sockaddr_in addr; socklen_t addrlen; struct connection *conn; int fd; addrlen = sizeof(addr); fd = accept(sock->fd.fd, (struct sockaddr *)&addr, &addrlen); if (fd < 0) { if (errno == EAGAIN) return; perror("accept"); exit(1); } conn = malloc(sizeof(*conn)); if (conn == NULL) { fprintf(stderr, "listening_socket_handler: memory allocation error, dropping connection"); close(fd); return; } IV_FD_INIT(&conn->fd); conn->fd.fd = fd; conn->fd.cookie = (void *)conn; conn->fd.handler_in = connection_handler; iv_fd_register(&conn->fd); IV_TIMER_INIT(&conn->disconnect_timeout); //XXXX iv_validate_now(); //XXXX conn->disconnect_timeout.cookie = (void *)conn; //XXXX conn->disconnect_timeout.handler = disconnect_timeout_expired;//XXXX conn->disconnect_timeout.expires = iv_now; //XXXX conn->disconnect_timeout.expires.tv_sec += CONNECTION_TIMEOUT;//XXXX iv_timer_register(&conn->disconnect_timeout); //XXXX } int main() { struct listening_socket s; struct sockaddr_in addr; int fd; fd = socket(AF_INET, SOCK_STREAM, 0); if (fd < 0) { perror("socket"); exit(1); } addr.sin_family = AF_INET; addr.sin_addr.s_addr = htonl(INADDR_ANY); addr.sin_port = htons(6667); if (bind(fd, (struct sockaddr *)&addr, sizeof(addr)) < 0) { perror("bind"); exit(1); } if (listen(fd, 4) < 0) { perror("listen"); exit(1); } iv_init(); IV_FD_INIT(&s.fd); s.fd.fd = fd; s.fd.cookie = (void *)&s; s.fd.handler_in = listening_socket_handler; iv_fd_register(&s.fd); iv_main(); iv_deinit(); return 0; } The global variable 'iv_now' contains the current time-of-day. However, it is updated lazily, and its contents might be stale at any given time. Before using it, iv_validate_now(3) must be called. EXAMPLE 4 The fourth example demonstrates how to use a custom fatal error handler that does not write the message to syslog. #include <stdio.h> #include <iv.h> static void fatal_error(const char *msg) { fprintf(stderr, "ivykis: FATAL ERROR: %s ", msg); } int main() { iv_init(); iv_set_fatal_msg_handler(fatal_error); iv_fatal("Programmatically triggered fatal error %d.", 42); printf("This code is never reached. "); iv_deinit(); return 0; } This program will abort immediately, with the error message printed to the standard error stream. SEE ALSO
ivykis(3), iv_fatal(3), iv_fd(3), iv_timer(3), iv_task(3), iv_init(3), iv_time(3) ivykis 2003-03-29 ivykis(3)
All times are GMT -4. The time now is 01:04 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy