Tcp Ip Server


 
Thread Tools Search this Thread
Top Forums Programming Tcp Ip Server
# 1  
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
# 2  
Old 01-30-2004
I guess you're asking how to time out a read. There are lots of ways. My choice would be to use select().

select can monitor your fd and return when a read will succeed without blocking. Then you just read the data. But select also can be give a timeout. And it will return when time expires.

It's so easy, it's almost cheating.

poll() is another system call that is similiar to select().

Another approach is to use setitimer() or alarm() to request a SIGALRM and some point in the future. Then just do the read. Assuming that the SA_RESTART option is off for SIGALRM, the system call will not be restarted. It will fail with a EINTR error.

You could also put the read inside a signal handler for SIGIO. Then use fcntl() to arrange for the SIGIO when data is available for reading.

But these ideas are all generic stuff and you want something in the tcp library. In that case you can turn keepalives on. But since keepalives must comply with the rfc's, you have very little control over them.

I still say select() rocks. But it's your program. Smilie
# 3  
Old 02-03-2004
Question

i definitely think select rocks too Smilie ...i just put it in the main a infinite loop with select inside


while (1) {
build_select_list();
timeout.tv_sec = 1;
timeout.tv_usec = 0;

readsocks = select(highsock+1, &socks, (fd_set *) 0,
(fd_set *) 0, &timeout);

if(readsocks>0)
read_socks();
}


and then accept the sock and go to read my buffer.

i also made every sock that connects nonblocking by setting the flags with fcntl(i don't know if it's the best thing)

In this case the server never ever block and that's good, but it happens sometimes the client (which is a control system with a readymade module for TCP IP client that i cannot change) goes up and down with the connection for hundreds times in a row before staying connected ... is the tcp ip so instable or there's something wrong?
anyway thanx again for your precious helping Smilie
# 4  
Old 02-03-2004
TCP/IP has security problems and if you try to break it you will probably succeed. Other than that it seems very stable to me.

The complaint in your first post was: you can't detect a dropped connection. That is a common complaint. A TCP connection can persist forever until it is explicitly shutdown. Even with keep-alives, it can last for hours.

Your new complaint is: connection goes up and down hundreds of times. That's an unusal complaint to make about TCP/IP. Either your server or the client is explicitly dropping the connection. And the client is choosing to reestablish a connection. I can't tell you why this is.
# 5  
Old 02-26-2004
I guess, the question is "How to find when the peer has closed its connection?"

There is a simple solution to find out when the other end of the socket has closed.

When the server reads from a closed socket, the read() call returns 0. This indicates that the client has terminated its connection.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

5. 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

6. 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

7. 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

8. 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

9. 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

10. 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
Login or Register to Ask a Question