| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
How to listens to two different ports at the same time
hi there,
i've got this code that listens for incomming message through port and display the message it recieves.. here's the code: Code:
#include "stdio.h"
#include "winsock.h"
#define PORT 1200
#define BACKLOG 4
struct sockaddr_in server;
struct sockaddr_in client;
int sockfd,sockfd2,n_bytes;
char msg[5000];
void handle_error(void);
int main(){
WSADATA wsda;
WSAStartup(0x0101,&wsda);
if((sockfd=socket(AF_INET,SOCK_STREAM,0))==-1){
handle_error();
printf("Socket error...");
return 0;
}
server.sin_addr.s_addr=INADDR_ANY;
server.sin_port=htons(PORT);
server.sin_family=AF_INET;
if(bind(sockfd,(struct sockaddr*)&server,sizeof(struct sockaddr))==-1){
handle_error();
printf("Cannot bind...");
return 0;
}
if(listen(sockfd,BACKLOG)){
handle_error();
printf("Error Listening...");
return 0;
}
while (1){
int size=sizeof(struct sockaddr_in);
if((sockfd2=accept(sockfd,(struct sockaddr*)&client,&size))==-1){
handle_error();
printf("Accept Error...");
return 0;
}
if((n_bytes=recv(sockfd2,msg ,5000,0))==-1){
handle_error();
printf("Error Recv...");
return 0;
}
msg[n_bytes]='\0';
printf("Client's Message:%s",msg);
}
WSACleanup();
closesocket(sockfd);
closesocket(sockfd2);
return 0;
}
void handle_error(void)
{
switch ( WSAGetLastError() )
{
case WSANOTINITIALISED :
printf("Unable to initialise socket.\n");
break;
case WSAEAFNOSUPPORT :
printf("The specified address family is not supported.\n");
break;
case WSAEADDRNOTAVAIL :
printf("Specified address is not available from the local machine.\n");
break;
case WSAECONNREFUSED :
printf("The attempt to connect was forcefully rejected.\n");
break;
case WSAEDESTADDRREQ :
printf("address destination address is required.\n");
break;
case WSAEFAULT :
printf("The namelen argument is incorrect.\n");
break;
case WSAEINVAL :
printf("The socket is not already bound to an address.\n");
break;
case WSAEISCONN :
printf("The socket is already connected.\n");
break;
case WSAEADDRINUSE :
printf("The specified address is already in use.\n");
break;
case WSAEMFILE :
printf("No more file descriptors are available.\n");
break;
case WSAENOBUFS :
printf("No buffer space available. The socket cannot be created.\n");
break;
case WSAEPROTONOSUPPORT :
printf("The specified protocol is not supported.\n");
break;
case WSAEPROTOTYPE :
printf("The specified protocol is the wrong type for this socket.\n");
break;
case WSAENETUNREACH :
printf("The network can't be reached from this host at this time.\n");
break;
case WSAENOTSOCK :
printf("The descriptor is not a socket.\n");
break;
case WSAETIMEDOUT :
printf("Attempt timed out without establishing a connection.\n");
break;
case WSAESOCKTNOSUPPORT :
printf("Socket type is not supported in this address family.\n");
break;
case WSAENETDOWN :
printf("Network subsystem failure.\n");
break;
case WSAHOST_NOT_FOUND :
printf("Authoritative Answer Host not found.\n");
break;
case WSATRY_AGAIN :
printf("Non-Authoritative Host not found or SERVERFAIL.\n");
break;
case WSANO_RECOVERY :
printf("Non recoverable errors, FORMERR, REFUSED, NOTIMP.\n");
break;
case WSANO_DATA :
printf("Valid name, no data record of requested type.\n");
break;
case WSAEINPROGRESS :
printf("address blocking Windows Sockets operation is in progress.\n");
break;
case WSAEINTR :
printf("The (blocking) call was canceled via WSACancelBlockingCall().\n");
break;
default :
printf("Unknown error.\n");
break;
}
}
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 ): Code:
#include "stdio.h"
#include "winsock.h"
#define PORT 1200
#define BACKLOG 4
struct sockaddr_in server;
struct sockaddr_in client;
int sockfd,sockfd2,n_bytes;
char msg[5000];
struct sockaddr_in servertwo;
struct sockaddr_in clienttwo;
int sock_one,secondn_bytes,sock_two;
char workmsg[5000];
#define workPORT 7777
#define workBACKLOG 4
void handle_error(void);
int main(){
WSADATA wsda;
WSAStartup(0x0101,&wsda);
if((sockfd=socket(AF_INET,SOCK_STREAM,0))==-1){
handle_error();
printf("Socket error...");
return 0;
}
server.sin_addr.s_addr=INADDR_ANY;
server.sin_port=htons(PORT);
server.sin_family=AF_INET;
if((sock_one=socket(AF_INET,SOCK_STREAM,0))==-1){
handle_error();
printf("Socket error...");
return 0;
}
servertwo.sin_addr.s_addr=INADDR_ANY;
servertwo.sin_port=htons(workPORT);
servertwo.sin_family=AF_INET;
if(bind(sockfd,(struct sockaddr*)&server,sizeof(struct sockaddr))==-1){
handle_error();
printf("Cannot bind...");
return 0;
}
if(listen(sockfd,BACKLOG)){ /*Listening */
handle_error();
printf("Error Listening...");
return 0;
}
if(bind(sock_one,(struct sockaddr*)&servertwo,sizeof(struct sockaddr))==-1){
handle_error();
printf("Cannot bind...");
return 0;
}
if(listen(sock_one,workBACKLOG)){
handle_error();
printf("Error Listening...");
return 0;
}
while (1){
int size=sizeof(struct sockaddr_in);
int size2=sizeof(struct sockaddr_in);
if((sockfd2=accept(sockfd,(struct sockaddr*)&client,&size))==-1){
handle_error();
printf("Accept Error...");
return 0;
}
if((n_bytes=recv(sockfd2,msg ,5000,0))==-1){
handle_error();
printf("Error Recv...");
return 0;
}
if((sock_two=accept(sock_one,(struct sockaddr*)&clienttwo,&size2))==-1){
handle_error();
printf("Accept Error...");
return 0;
}
if((secondn_bytes=recv(sock_two,workmsg ,5000,0))==-1){
handle_error();
printf("Error Recv...");
return 0;
}
msg[n_bytes]='\0';
workmsg[secondn_bytes]='\0';
printf("Client 1's Message:%s",msg);
printf("Client 2's Message:%s",workmsg);
}
WSACleanup();
closesocket(sockfd);
closesocket(sockfd2);
closesocket(sock_one);
closesocket(sock_two);
return 0;
}
void handle_error(void)
{
switch ( WSAGetLastError() )
{
case WSANOTINITIALISED :
printf("Unable to initialise socket.\n");
break;
case WSAEAFNOSUPPORT :
printf("The specified address family is not supported.\n");
break;
case WSAEADDRNOTAVAIL :
printf("Specified address is not available from the local machine.\n");
break;
case WSAECONNREFUSED :
printf("The attempt to connect was forcefully rejected.\n");
break;
case WSAEDESTADDRREQ :
printf("address destination address is required.\n");
break;
case WSAEFAULT :
printf("The namelen argument is incorrect.\n");
break;
case WSAEINVAL :
printf("The socket is not already bound to an address.\n");
break;
case WSAEISCONN :
printf("The socket is already connected.\n");
break;
case WSAEADDRINUSE :
printf("The specified address is already in use.\n");
break;
case WSAEMFILE :
printf("No more file descriptors are available.\n");
break;
case WSAENOBUFS :
printf("No buffer space available. The socket cannot be created.\n");
break;
case WSAEPROTONOSUPPORT :
printf("The specified protocol is not supported.\n");
break;
case WSAEPROTOTYPE :
printf("The specified protocol is the wrong type for this socket.\n");
break;
case WSAENETUNREACH :
printf("The network can't be reached from this host at this time.\n");
break;
case WSAENOTSOCK :
printf("The descriptor is not a socket.\n");
break;
case WSAETIMEDOUT :
printf("Attempt timed out without establishing a connection.\n");
break;
case WSAESOCKTNOSUPPORT :
printf("Socket type is not supported in this address family.\n");
break;
case WSAENETDOWN :
printf("Network subsystem failure.\n");
break;
case WSAHOST_NOT_FOUND :
printf("Authoritative Answer Host not found.\n");
break;
case WSATRY_AGAIN :
printf("Non-Authoritative Host not found or SERVERFAIL.\n");
break;
case WSANO_RECOVERY :
printf("Non recoverable errors, FORMERR, REFUSED, NOTIMP.\n");
break;
case WSANO_DATA :
printf("Valid name, no data record of requested type.\n");
break;
case WSAEINPROGRESS :
printf("address blocking Windows Sockets operation is in progress.\n");
break;
case WSAEINTR :
printf("The (blocking) call was canceled via WSACancelBlockingCall().\n");
break;
default :
printf("Unknown error.\n");
break;
}
}
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
|
|||
|
|||
|
finally found the solution...
although the program sometimes hungs when i try to exit it... anyways thats fine with me... btw here is the code Code:
#include <stdio.h>
#include <winsock.h>
#include <process.h>
#define PORT 1200
#define BACKLOG 4
#define workPORT 7777
#define workBACKLOG 4
#define STRSIZE 16536
#define STRSIZETWO 16536
struct sockaddr_in server;
struct sockaddr_in client;
int sockfd,sockfd2,n_bytes;
char msg[5000];
struct sockaddr_in servertwo;
struct sockaddr_in clienttwo;
int sock_one,secondn_bytes,sock_two;
char workmsg[5000];
int recieveMessage(void);
int recieveMessageTwo(void);
void handle_error(void);
void errexit(const char *format, ...)
{
va_list args;
va_start(args, format);
vfprintf(stderr, format, args);
va_end(args);
WSACleanup();
exit(1);
}
int main(){
WSADATA wsda;
WSAStartup(0x0101,&wsda);
if((sockfd=socket(AF_INET,SOCK_STREAM,0))==-1){
handle_error();
printf("Socket error...");
return 0;
}
server.sin_addr.s_addr=INADDR_ANY;
server.sin_port=htons(PORT);
server.sin_family=AF_INET;
if((sock_one=socket(AF_INET,SOCK_STREAM,0))==-1){
handle_error();
printf("Socket error...");
return 0;
}
servertwo.sin_addr.s_addr=INADDR_ANY;
servertwo.sin_port=htons(workPORT);
servertwo.sin_family=AF_INET;
if(bind(sockfd,(struct sockaddr*)&server,sizeof(struct sockaddr))==-1){
handle_error();
printf("Cannot bind...");
return 0;
}
if(bind(sock_one,(struct sockaddr*)&servertwo,sizeof(struct sockaddr))==-1){
handle_error();
printf("Cannot bind...");
return 0;
}
if(listen(sockfd,BACKLOG)){
handle_error();
printf("Error Listening...");
return 0;
}
if(listen(sock_one,workBACKLOG)){
handle_error();
printf("Error Listening...");
return 0;
}
while (1){
if (_beginthread((void (*)(void *)) recieveMessage, STRSIZE, ( void *) sockfd) < 0)
{
handle_error();
errexit("_beginthread: %s\n", strerror(errno));
}
if (_beginthread((void (*)(void *)) recieveMessageTwo, STRSIZETWO, ( void *) sock_one) < 0)
{
handle_error();
errexit("_beginthread: %s\n", strerror(errno));
}
}
WSACleanup();
closesocket(sockfd);
closesocket(sockfd2);
closesocket(sock_one);
closesocket(sock_two);
return 0;
}
int recieveMessage(){
int size=sizeof(struct sockaddr_in);
if((sockfd2=accept(sockfd,(struct sockaddr*)&client,&size))==-1){
printf("Accept Error...");
}
if((n_bytes=recv(sockfd2,msg ,50,0))==-1){
printf("Error Recv...");
}
msg[n_bytes]='\0';
printf("Client's Message:%s",msg);
return 0;
}
int recieveMessageTwo(){
int size2=sizeof(struct sockaddr_in);
if((sock_two=accept(sock_one,(struct sockaddr*)&clienttwo,&size2))==-1){
printf("Accept Error...");
}
if((secondn_bytes=recv(sock_two,workmsg ,5000,0))==-1){
printf("Error Recv...");
}
workmsg[secondn_bytes]='\0';
printf("Client 2's Message:%s",workmsg);
return 0;
}
void handle_error(void)
{
switch ( WSAGetLastError() )
{
case WSANOTINITIALISED :
printf("Unable to initialise socket.\n");
break;
case WSAEAFNOSUPPORT :
printf("The specified address family is not supported.\n");
break;
case WSAEADDRNOTAVAIL :
printf("Specified address is not available from the local machine.\n");
break;
case WSAECONNREFUSED :
printf("The attempt to connect was forcefully rejected.\n");
break;
case WSAEDESTADDRREQ :
printf("address destination address is required.\n");
break;
case WSAEFAULT :
printf("The namelen argument is incorrect.\n");
break;
case WSAEINVAL :
printf("The socket is not already bound to an address.\n");
break;
case WSAEISCONN :
printf("The socket is already connected.\n");
break;
case WSAEADDRINUSE :
printf("The specified address is already in use.\n");
break;
case WSAEMFILE :
printf("No more file descriptors are available.\n");
break;
case WSAENOBUFS :
printf("No buffer space available. The socket cannot be created.\n");
break;
case WSAEPROTONOSUPPORT :
printf("The specified protocol is not supported.\n");
break;
case WSAEPROTOTYPE :
printf("The specified protocol is the wrong type for this socket.\n");
break;
case WSAENETUNREACH :
printf("The network can't be reached from this host at this time.\n");
break;
case WSAENOTSOCK :
printf("The descriptor is not a socket.\n");
break;
case WSAETIMEDOUT :
printf("Attempt timed out without establishing a connection.\n");
break;
case WSAESOCKTNOSUPPORT :
printf("Socket type is not supported in this address family.\n");
break;
case WSAENETDOWN :
printf("Network subsystem failure.\n");
break;
case WSAHOST_NOT_FOUND :
printf("Authoritative Answer Host not found.\n");
break;
case WSATRY_AGAIN :
printf("Non-Authoritative Host not found or SERVERFAIL.\n");
break;
case WSANO_RECOVERY :
printf("Non recoverable errors, FORMERR, REFUSED, NOTIMP.\n");
break;
case WSANO_DATA :
printf("Valid name, no data record of requested type.\n");
break;
case WSAEINPROGRESS :
printf("address blocking Windows Sockets operation is in progress.\n");
break;
case WSAEINTR :
printf("The (blocking) call was canceled via WSACancelBlockingCall().\n");
break;
default :
printf("Unknown error.\n");
break;
}
}
i'm using vc6 and my os is win2k also include the WSOCK32.lib and make it multithreaded in the "USe run time Library" any comment here will be much appreciated ( especially if you some wrong convention that i've used ) - jaro |
|
#3
|
|||
|
|||
|
dumb question
its being a while since i dealed with sockets..
but i have to ask shouldnt the listening being done in each thread? i mean in this way you v done it; the prgram cant listen for two connections at the same time doesnt it? let me know if i am wrong |
|
#4
|
||||
|
||||
|
I think I would have to agree and I wouldn't see the reason why you would need to listen to two separate ports, Because If I'm not mistaken it is possible to have two or more clients connected through a single port, hence the extra open port would be unnecessary. But just my opinion.
__________________
---Official Member Of The Itsacon Fan Club--- ![]() Give a man a fish and he will eat for a day. Teach a man to fish and he will sit in a boat all day drinking beer. |
|
#5
|
|||||
|
|||||
|
I have just written a program (for Linux) that you might find useful.
you could easily create a seperate thread to handle each port and further threads to handle each client. My program only does one port and you won't be able to execute it as you will also require a custom module. Important stuff is around line #46, although it'll prob confuse you a bit as you're writing for Windows. C Code:
|
![]() |
| Viewing: Dev Articles Community Forums > Programming > C/C++ Help > How to listens to two different ports at the same time |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|