Programming :: Socket Server Using Pthread Doesn't Wait For Accept() Properly?

Apr 19, 2010

I'm building a simple(?) socket server using threads to serve up a few requests. The spec is such that I have to listen to three ports at once, so I decided to use pthread to create three separate threads that would wait for connections, then spawn new threads to handle them.

The problem is that when I do this, for some reason the program never enters the wait loop and instead terminates (All three threads did get created since the messages get printed properly.) It gets to the line which prints "???", but not the line after the accept() call.I don't see an open port when I check for one either so I'm 99% sure they're terminating.Basically I have a main() method which has three calls to pthread_create, which should result in three threads being run that all wait for connections (listenOnPort). After each thread creation I print some info to make sure it's actually being created.

By the way, when I just run listenOnPortwithout threading, the server appears to enter the loop correctly and seems to be waiting for requests. It's only when I run the functions as threads that the problem seems to happen.The source is attached below. Any help will be appreciated. Much of the code is borrowed from a website (I can't post it because I am new here.) You need not worry about the handler_ methods because those are just methods that are run by the threads themselves.

Also--the original source was in C and I changed it to C++. Should I just use C?
server.h Code: /*
* server.h

[code]....

View 1 Replies


ADVERTISEMENT

Programming :: Timer In Socket Programming - Wait For X Sec After Read() And Then Disconnect The Client Connection

Mar 8, 2011

I have a server listening on incoming client connections. Once the client establishes SSL connection with the server, the server waits on read() from the client. Only Client can disconnect the connection. I want to have a timer in the server program to wait for x secs after read() and then disconnect the Client connection.

View 3 Replies View Related

CentOS 5 :: Socket Programming: Accept()-ing All Pending Connections

May 28, 2009

I am writing a server which uses edge-triggered epoll. When the server calls accept(), it just extracts the first connection in the pending queue. If there are more connections that are waiting, can we make the server accept all the pending connections? I wrote a loop like the following:

do {
client_fd = accept(...);
/* Work with the client fd */
} while (client_fd != -1); [/code]

Doesn't seem to work. A related question, more a clarification, is as follows. My understanding was that a connect() at the client returns only when accept() in the server returns. Clearly, I was incorrect. Even with no accept() call in the server, my client was able to connect and send data. Am I getting this right?

View 2 Replies View Related

Programming :: Socket And Timer Programming - Server Doesn't Execute Any Msg Which Client Sent

May 16, 2011

i have a server program which accept multiple client connection and am using polling. like every 2 secs it will look to client whether any data is received after it binded. i have used setitimer but there is runtime error i got.. the server accept all client connection but doesn't execute any msg which client sent.

#include<stdio.h>
#include<stdlib.h>
#include<sys/socket.h>
#include<sys/types.h>
#include<netdb.h>
#include<unistd.h>
#include<pthread.h>
#include<string.h>
[Code]....

View 1 Replies View Related

Programming :: C++ Cin Doesn't Wait For Input By Operator

Jan 4, 2010

I am trying to learn C++.I implemented a simple archive program, and I am in a situation in which the user is prompted by a menu to make a choice.So I have some cout instruction to illustrate the possible choices and then

int choice;
cin>>choice;

and everything works fine.I introduced this code in a "while" loop that checks wether the choice made by the user is valid or not:

bool check=true;
int choice;
while(check)
{
cin>>choice;
if(the choice is valid) {...;check=false}
else cout<<"please make an other choice"
}

What is happening is that if by mistake the user introduces a character in place of a number, the loop repeats indefinitely because the program, when it get to the "cin" instruction, does not pauses to wait for a new input.

View 7 Replies View Related

Programming :: Sed Doesn't Accept Variables To Replace In File

Mar 17, 2011

[code]...

The variables are clean and available in my script. No prob. $old is an IP-address of a server, $new as well. replacement-file is an xml-file (the ip-address to be changed is within an xml-tag). It should be trivial, but it isn't. I spent the last 3 hours reading about the problem, trying several different 'solutions' or workarounds. It should work, the '"' should take care that the variables get expanded, but whatever I try the old server IP isn't replaced.

View 7 Replies View Related

Programming :: Socket Programming - Detect Whether A Client Socket Is Closed / Active?

Mar 8, 2011

iam just trying to connect to server which accepts one client and server will read(blocking operation) infinitely, but After closing the client socket the server "read operation" is returning zero and "errno variable(in errno.h)" value is also zero. how can i detect whether a client socket is closed/active..?

client.c
Serv_Addr.sin_family = AF_INET;
Serv_Addr.sin_addr.s_addr = inet_addr("127.0.0.1");
Serv_Addr.sin_port = htons(26553);
if( 0 > connect (Serv_Fds,&Serv_Addr,sizeof(Serv_Addr)) )
{
perror("connect");
return 0;
[Code]....

View 3 Replies View Related

Programming :: Socket Programming - Connect To An IRC Server In A C

Mar 16, 2010

I am looking to be able to connect to an IRC server in a C program. I have followed some instructions from an old book I have however it doesn't seem to be working. I've tried connecting to localhost on my ssh port and it still doesn't want to work. This is what I have so far as a piece of skeleton test code:

Code:
#include<sys/types.h>
#include<stdio.h>
#include<stdlib.h>
#include<sys/socket.h>
#include<unistd.h>
#include<netinet/in.h>
#include<arpa/inet.h>
[Code]....

View 1 Replies View Related

General :: Socket Programming - Feed Some Words One By One To The Socket

Jul 6, 2010

i'm writing a simple program of client socket program. Here below is the code sample which i'm writing...

Code:
//tcp_client.c
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include<string.h>
#include<stdlib.h>
[Code].....

the above code works fine.if we copmile and run ./a.exe 192.xx.xx.xxx 1111 and press enter it works fine..everytime it asks "Please enter the message: " and if give that will be displayed in server. but my problem is i dont want to print everytime "Please enter the message: " i just want to feed some words one by one to the socket.

View 4 Replies View Related

Programming :: Setsockopt : Socket Operation On Non-socket Getting Error?

Mar 30, 2009

im getting that error in my code for some reason. I compiled my code, and when i try to run this server it throws me an error on my call to setsocketopt(). The only way it can reach that part of my loop is if it succeeds when it calls sock() so I dont understand why the error says its an operation on a non-socket. Im just trying to set up a server to pass messages from a client to it a viceversa. Here is the code:

Code:
int main()
{
int socket_fd, new_socket_fd, k;
struct addrinfo hints, *server_info, *p;
struct sockaddr_storage peer_address;
code....

View 7 Replies View Related

Programming :: Chat Socket Between Client And Server

Mar 17, 2011

I want to built a Chat Program (based on Socket connection) between client and server. I use the GTK+ &GLADE graphic tool. Can you send me the code for this program or any material to learn. Also, I want to know how to show the input to text view (in GTK+&Glade).

View 3 Replies View Related

Red Hat / Fedora :: Properly Define Iptables To Accept Traffic Only From Specified Hosts?

Nov 8, 2010

I have an log monitoring application that is listening on port 514 to receive events only from certain hosts.In order to control this,I've tried set up iptables to define those hosts that are allowed to this application. Here is an example of the script that contain the commands:

iptables.sh -> Code: iptables -I INPUT -p tcp -s 192.168.0.10/24 --dport 514 -j ACCEPT
iptables -I INPUT -p tcp -s 192.168.0.15/24 --dport 514 -j ACCEPT
...

[code]....

View 1 Replies View Related

Programming :: How Could Server Detect Closed Client Socket Using TCP And C++

Aug 6, 2010

Reading some examples on net and copying some codes I was able to build a (frankstein) server that accept connection from one client and receive and send messages to it. The big problem that shows up is that I don't know when client disconnects from my server.

I've been looking for a solution, but no success. I'd read about SO_KEEPALIVE option (which could solve my problem), but I don't know how to use it (how to check the value of it).

I can't use ping because the server (machine) could be running, but not the client (software).

Anyone knows a good tutorial or how to (for beginners like me xD) of TcpIp sockets using c/c++ and how to detect when a client disconnect?

I don't know sockets very well.

View 6 Replies View Related

Programming :: Server Socket Assigned By Children Processes ?

Aug 27, 2010

I am currently programming a suite of daemons in c++ on CentOS 64. More or less all those daemons are SOAP servers, with the SOAP part handled by gSOAP.

One of the daemons is a monitor sitting on a certain server and more or less just checking if the other daemons are up and running - but also offering a SOAP interface to get the 'current situation' on the machine.

Whenever the detected situation does not correspond to the defined (i.E. a process which should be is not running) it is started up.

This part, I implemented via a system() call that starts the other process.

I was a little confused when lately often this monitoring process would not start because it could not bind its server port - the one the SOAP server is listening on.

And confusion got bigger when netstat told me that the port is assigned to one of the processes that were before started by the monitor..?

Even better: if I then kill the process of which netstat tells me it holds the port, the next netstat shows the next monitor-started process as the one listening on the port..

Only if I kill all the processes that were started by the monitor, the port is released and I am able to launch the monitor again.

Now I wonder: why does this port get 'reassigned' to the children - and most of all: how can I prevent this from happening?

View 6 Replies View Related

Programming :: Socket C++ / Send And Receive Messages In Server?

Jan 2, 2009

I'm trying implement a small server for a university work, my server is creating, binding and listening but it don't accept any connection, these are the files I'm using in server:

Code:

// Definition of the Socket class
#ifndef SOCKET_H
#define SOCKET_H
#include <sys/types.h>
code....

I've made a client just for test it, but no ones run well, the server is always returning -1 when accept is called!!

What should I do to send and receive messages in this server??

View 3 Replies View Related

Server :: Socket Connections / Socket And TCP Connection For Connecting To An External System?

Dec 7, 2010

Anyone knows the difference between a socket and TCP connection for connecting to an external system?

View 1 Replies View Related

Programming :: Remote Function With Pthread?

May 22, 2010

I wrote some code in c, using pthread (I configured the linker and compiler in eclipse IDE first).

Code: #include <pthread.h>
#include "starter.h"
#include "UI.h"
Page* MM;

[Code]....

View 6 Replies View Related

Programming :: Pthread Not Release Memory

Jan 1, 2011

I have faced a problem with my code (a small tcp server). After the thread returns, the memory not decreases, but when a new connection is made, memory not increases and the new connection initialize a new thread with a same thread id of the previous thread. When two connections are made at same time, two thread ids are created and when the respective threads returns the memory not decreases. The Valgrind indicates that not memory leak occurs, the pointers are released. Indeed, more memory are allocate when new thread id is created. i used gcc and debian.

[Code]....

View 1 Replies View Related

Programming :: Signal Handling In Pthread?

Mar 12, 2011

I have created a pthread, and installed a signal handler inside that, same way as we do in main( ) function. The thread's signal handler is a separate function. Surprisingly, it is not working, that is the thread's signal handler is not able to catch signals. Here is the code:

Code: #include <unistd.h>
#include <sys/types.h>
#include <stdio.h>
#include <signal.h>
typedef struct data

[Code]...

View 1 Replies View Related

Programming :: Pthread Memory Not Being Freed?

Apr 19, 2010

I have an application that has pretty large memory profile. The general program flow is as follows:

Code:
main:
do 10 times:

[code]....

View 9 Replies View Related

Ubuntu :: 8.10 Server Apt-get Doesn't Work Properly

Apr 25, 2011

I am trying to install some packages in a ubuntu 8.10 server, however, apt-get doesn't work (I get several 'not found' errors). Is there any way to do that? I know this version is probably not supported anymore but I can't just format a customer server because it's OS is outdated...I just can't put my sources.list on it because I am on ubuntu 10.10 and it would download updates for the wrong OS probably causing a huge damage on the old one.

View 9 Replies View Related

Programming :: Portable Pthread Library Out Of 10 Distros?

Mar 10, 2011

im writing a cross-platform software (linux/solaris/mac/bsd) so i was wondering, how portable is pthread library? out of 10 linux distros, how many will have it by default? and does it exists on mac/solaris/bsd by default? or user will have to install it?

View 6 Replies View Related

Programming :: Suspend/resume A Thread When Using Pthread.h?

Dec 14, 2010

I want to suspend/resume a thread. The library I am using is pthread.h.I am also running my application on linux.Is there any function in pthread. let me suspend a thread temporary?I have read a document in which it was mentioned thatthread does not support suspend/resume

View 4 Replies View Related

Programming :: Pthread Mapping From Compiler To Kernel?

Aug 20, 2010

I'm trying to figure out how pthreads are mapped from the compiler to the Linux kernel. The pthread prototypes are found in a compiler header file (pthread.h), yet the kernel would be responsible for scheduling the threads. So, how does the compiler resolve the pthread symbols at compile time?

View 1 Replies View Related

Programming :: Pthread And Mutex With Short Unlock Time?

Jun 15, 2010

I am wondering if pthread could make that a single thread keeps the "mutex" all the time if the time it remains unlocked is very small.

thread1
{
while (1)
{
lock; do_task(); unlock();

[code]...

I experiment the thread2 never getting access to the mutex and never printing the nice message. I would expect that once thread2 calls "lock", it would get the mutex as soon as thread1 calls unlock() but it does not seem to be the case. If I add a sleep of some microseconds (100) in thread1 after unlocking the mutex, it solves the problem.

Does anyone know if this behaviour is normal? Is there a way to configure my mutex so that thread2 receives it when unlocked?I use

pthread_mutexattr_settype(&att, PTHREAD_MUTEX_RECURSIVE_NP);
pthread_mutex_init(&handle, &att);

to create my mutex. I am running ubuntu 9.04

View 3 Replies View Related

Programming :: Use Pthread Scheduling Policy Without Root User?

Jan 12, 2011

I am working on a large program in C to run on Puppy Linux. I have multiple pthreads running. I want to be able to set the pthread SCHED_POLICY and priority in my program but I want a user to be able to run the program without root privilege.
Using a sticky bit and root ownership gives the user too much power because they will be able to write and compile their own scripts. Is there a way to use 'sudo' when I set the thread parameters from my program or something like this?

View 1 Replies View Related

Programming :: Pthread - Optimize Computing Function / Loop With Threads?

Sep 4, 2010

I have to compute prime numbers with threads in C. So I choose the algorithm "Sieve of Eratosthenes" and I tried to create 2 threads, but I don't know how to optimize my computing function with threads I beg for help. This is my function:

[Code]...

but it seems like the both of the threads are calculating the same function twice? Is it possible to be optimized or not?

View 7 Replies View Related

Programming :: Rules To Set The Pthread Stack Size - Compute The Memory Needed By Each Thread?

Jul 26, 2010

i have an application that launches several pthreads, i know that the default size used by Linux is 8Mb for each pthread. However i would like to optimize the total memory usage by my application by decreasing the default stack size of each pthread to the needed resources. My questions:

- Are there any rules to set the pthread stack size.
- How to compute the memory needed by each thread.
- Is the malloc call inside a thread counted from the stack size of the same pthread?

View 2 Replies View Related

Ubuntu Multimedia :: Doesn't Accept DVD-RWs?

Aug 12, 2010

there's one major issue left preventing Ubuntu from running flawlessly on my hine.Ubuntu doesn't recognize DVD-RWs once written on. The problem occurred with my old drive (LG GSA-H55N) as well with my new drive (Samsung SH-S222L).I'm using Sony DVD-RW (1x-2x speed)
Ubuntu only recognizes blank DVD-RWsonce written on - no matter whether written by Brasero or Windows 7 - Ubuntu isn't showing any drive in Nautilus nor in Brasero after putting in the DVDtherefore I can't even erase themboth of the drives as well as the DVD-RWs are working perfectly fine on Windows 7 (erasing, writing)I managed to get all other things working by now. My system is close to perfection and this is the only remaining major issue.

View 9 Replies View Related

Programming :: Read From Stdin In Perl Do Not Wait For EOF?

Apr 28, 2010

I am trying to read one line at a time in perl and not wait for stdin eof to start the action:

Code:
foreach(<STDIN>){
<do something>
}

if i pipe ls into it, it waits for ls to be done before doing the action.

View 1 Replies View Related







Copyrights 2005-15 www.BigResource.com, All rights reserved