![]() |
|
#1
|
||||
|
||||
How to listens to two different ports at the same timehi there,
i've got this code that listens for incomming message through port and display the message it recieves.. here's the code: CPP / C++ / C Code:
i've run/tested this and it works fine... ...now i want for the program to listens to two different ports and display the message it recieves through them (ports)... so i've try to edit the code above to this ( not quite sure if it the right thing to do though ): CPP / C++ / C Code:
now the problem when i run the program and try to send a message it does nothing. it does not display the message sent or display some error ( if any ). can anyone help me shed light to this, or better yet give me a link in "How to listens to two different ports at the same time using C" i've tried to google it but the results a not that promising. sorry for the long post. any comment will be much appreciated btw: i'm using mvc 6 and win2k thanks in advance, jaro |
|
#2
|
||||
|
||||
Re: How to listens to two different ports at the same timeIt compiles ok, but when trying to build the code, I get :
Code:
Anyway, here is an example for working with sockets Dave posted : CPP / C++ / C Code:
It demonstrates working with sockets in a simple way. I posted this example Dave wrote for the benefit of all. I hope it will help you. Kobi. __________________
It's actually a one time thing (it just happens alot). |
|
#3
|
||||
|
||||
Re: How to listens to two different ports at the same timeQuote:
oops forgot to tell that you should include WINSOCK32.LIB in the project and in the project setting under code generaton set run time library to Multithreaded... |
|
#4
|
||||
|
||||
Re: How to listens to two different ports at the same timefinally found the solution...
although the program sometimes hungs when i try to exit it... anyways thats fine with me... btw here is the code CPP / C++ / C Code:
any comment here will be much appreciated ( especially if you some wrong convention that i've used ) - jaro |
|
#5
|
|||
|
|||
Re: How to listens to two different ports at the same timeHi,
I just looked at your code. I have a doubt. Parden me if i am wrong. I am not a windows person. In your infinite while loop you are starting a new thread for each loop run. Arn't you suppose to start one thread for each socket and then in thread handler run an infinite loop to receive messages.? In your thread handler you are accepting on the socket passed as an argument. If I am right, you are creating thousands of unneccessary threads. All you need is 2 threads. You need to run accept and recv in infinite loop like below CPP / C++ / C Code:
You dont have to write two different functions for each thread which essentially does the same job. You can use above function for both the threads. Just pass different socket discriptors to each thread while starting it. Inside a thread, you can identify a thread by its Id. There are methods to retrieve this id. On *nix using POSIX threads (pthreads), function pthread_self() returns the thread id of the current thread. You can identify which thread received the message using this id. Or you can pass a data structure to the thread as an argument which will have information abt whether its a client or server thread alongwith the socket descriptor, which you can use inside the theread function to print the appropriate message. |
|
#6
|
||||
|
||||
Re: How to listens to two different ports at the same timehi nkhambal,
thanks for your comment and for pointing out some things that i failed to see ... (now i know the reason why program slow down my system i'll make the necessary changes in my code and i will post it to this thread... many thanks, jaro |
Recent GIDBlog
Meeting the local Iraqis by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Creating a time converter | tylerfelix | C++ Forum | 15 | 20-Nov-2005 20:34 |
| Simulation Problem | wu_weidong | C++ Forum | 7 | 12-Mar-2005 22:56 |
| [CONTEST?]Data Structure Test | dsmith | C Programming Language | 2 | 06-Jun-2004 15:13 |
| time Problem | zuzupus | MySQL / PHP Forum | 9 | 24-Jul-2003 07:02 |
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The