Jump to content

  • Log In with Google      Sign In   
  • Create Account

Need help with opening port in winsock2


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
9 replies to this topic

#1 Xeno   Members   -  Reputation: 122

Like
Likes
Like

Posted 01 July 2000 - 09:08 AM

ok whats wrong with this code: SOCKET s = socket (AF_INET, SOCK_STREAM, 0); // Create socket sockaddr_in addr; // the address structure for a TCP socket addr.sin_family = AF_INET; // Address family Internet addr.sin_port = htons (5001); // Assign port 5001 to this socket addr.sin_addr.s_addr = htonl (INADDR_ANY); // No destination if (bind(s, (LPSOCKADDR)&addr, sizeof(addr)) == SOCKET_ERROR) { // error WSACleanup (); // unload WinSock return; // quit } the "bind" function allways fail for some reason.... tnx roy. Posted By Xeno. Kobe Bryant - "Just believe in yourself" Visit my web site : VSoft

Sponsor:

#2 win32mfc   Members   -  Reputation: 122

Like
Likes
Like

Posted 01 July 2000 - 09:50 AM


Your socket creation isn''t right..

You have:
SOCKET s = socket (AF_INET, SOCK_STREAM, 0); // Create socket

You need:
SOCKET s = socket (AF_INET, SOCK_STREAM, IPPROTO_TCP); // Create socket

The following is some code I wrote that works..

struct sockaddr_in SA;
::ZeroMemory(&SA, sizeof(sockaddr_in));
SA.sin_family = AF_INET;
SA.sin_port = htons(nPort);
// memcpy(&(SA.sin_addr.s_addr), HostEnt->h_addr_list[0], 4);

cout << "Creating Socket" << endl;

int hSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if(hSocket == 0)
{
cout << "Error: Error Creating Socket For Port" << nPort << endl;
WSACleanup();
return(1);
}

cout << "Binding Socket" << endl;

if(bind(hSocket, (struct sockaddr*) &SA, sizeof(sockaddr_in)) != 0)
{
cout << "ERROR: Error Binding Socket (code " << WSAGetLastError() << ")" << endl;
closesocket(hSocket);
WSACleanup();
return(1);
}


// CHRIS


#3 Xeno   Members   -  Reputation: 122

Like
Likes
Like

Posted 01 July 2000 - 10:19 AM

tnx its working now , i have more question:
by referd to the code i posted in my last post , what wrong here:
// WSAStartup () has been called
// SOCKET s is valid
// s has been bound to a port using sockaddr_in sock
sockaddr_in target;

target.sin_family = AF_INET; // address family Internet
target.sin_port = htons (5001); // set server’s port number
target.sin_addr.s_addr = inet_addr ("52.123.72.251"); // set server’s IP

if (connect(s, target, sizeof(target)) == SOCKET_ERROR)
{ // an error connecting has occurred!
WSACleanup ();
return;
}


tnx xeno.

Posted By Xeno.
Kobe Bryant - "Just believe in yourself"

Visit my web site : VSoft

#4 win32mfc   Members   -  Reputation: 122

Like
Likes
Like

Posted 01 July 2000 - 11:00 AM

Hiya,
First, you don''t need to bind() if you are going to connect.
You only need to do the socket() and then connect().

Here''s code that works; though it does a host lookup for the server address first.

// CHRIS

struct hostent* HostEnt;
if((HostEnt = ::gethostbyname(ServerName)) == NULL)
{
sprintf(rvConnectString, "ERROR: Unable To Resolve Host Name ''%s''", ServerName);
return(0);
}

struct sockaddr_in SA;
SA.sin_family = AF_INET;
SA.sin_port = htons(nPort);
memcpy(&(SA.sin_addr.s_addr), HostEnt->h_addr_list[0], 4);
int hSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if(hSocket == 0)
{
strcpy(rvConnectString, "ERROR: Can''t Create Socket");
return(0);
}

char szLine[1024];

if(connect(hSocket, (struct sockaddr*) &SA, sizeof(SA)) != 0)
{
strcpy(rvConnectString, "ERROR: Can''t Connect To Server");
return(0);
}


#5 Xeno   Members   -  Reputation: 122

Like
Likes
Like

Posted 01 July 2000 - 11:23 AM

u right, i dont need to bind , but what if i want to connect to the local system , i mean 127.0.0.1 , i hate to open a port.

and still the function fails , im try to connect to 127.0.0.1 and its fails , im opening port 5555 and trying to connect port 5555 and its not working....

Posted By Xeno.
Kobe Bryant - "Just believe in yourself"

Visit my web site : VSoft

#6 Xeno   Members   -  Reputation: 122

Like
Likes
Like

Posted 01 July 2000 - 11:25 AM

u right, i dont need to bind , but what if i want to connect to the local system , i mean 127.0.0.1 , i hate to open a port.

and still the function fails , im try to connect to 127.0.0.1 and its fails , im opening port 5555 and trying to connect port 5555 and its not working....

Posted By Xeno.
Kobe Bryant - "Just believe in yourself"

Visit my web site : VSoft

#7 Xeno   Members   -  Reputation: 122

Like
Likes
Like

Posted 01 July 2000 - 11:30 AM

hhmmm.... why it was posted twise, strange .

Posted By Xeno.
Kobe Bryant - "Just believe in yourself"

Visit my web site : VSoft

#8 win32mfc   Members   -  Reputation: 122

Like
Likes
Like

Posted 01 July 2000 - 11:43 AM

Are you calling listen()?

Create 1 socket that listens on a port:
socket() ... bind() ... listen()

Then another socket to connect to it..
socket() ... connect()

// CHRIS


#9 Xeno   Members   -  Reputation: 122

Like
Likes
Like

Posted 01 July 2000 - 12:07 PM

Tnx u really helped me a lot
the connect() function working now , but i still have just one more question , i will be very thankfull if u can answer it too :

now, i want to send and recive data , am i need to create new sockets for it too?

tell me if im doing it right:

s3 = socket (AF_INET, SOCK_STREAM, 0);
s4 = socket (AF_INET, SOCK_STREAM, 0);

this one im puting in the main loop:

recv (s3, buffer, sizeof(buffer), 0);
if(buffer == "mb")
CApp.DisplayMassegeBox("data recived","data:",MB_OK);

and im send data like that:

// SOCKET s is initialized
char buffer[11]; // buffer that is 11 characters big
sprintf (buffer, "mb");
send (s4, buffer, sizeof(buffer), 0);

am i doing it right?

tnx roy

#10 win32mfc   Members   -  Reputation: 122

Like
Likes
Like

Posted 01 July 2000 - 12:49 PM

Hiya.. Well, you have to be very careful with send() and recv().
If you send() 100 bytes, you may not recv() 100 bytes in one shot. You may only receive 60 bytes on the first recv() and then get the next 40 bytes on the next recv(). You never really know. Though, in practice, you''ll usually get small packet sizes in one shot [esp if it''s only two bytes long].

Once you have a socket() and connect(), you use that same socket to talk.. use send() and recv() to write and read bytes respectively. Be sure that you send() the correct number of bytes.

Also, always check your send() and recv() return values for SOCKET_ERROR.

I don''t know what sort of protocol you are devising. Many internet protocols like NNTP (News), SMTP, POP3, etc, are line-oriented. You send a full LINE of text and receive LINEs of text. Perhaps you are defining something else; more binary (object) oriented. The nice property of line-oriented protocols is that they are relatively easy to debug/play with.. Just run TELNET and connect to your server.

You can do that with some functions like the one''s I''ll append below. One to send() a line of data and another to recv() a line of data.

The GetLine() function below is not optimized for heavy data flow. Since it reads one character at a time, you''ll want to re-implement it so that it reads in larger chunks into a buffer and then scans the buffer.

// CHRIS

//
// Call it like this: rv = SendLine(hSocket, "USER win32mfc\r\n");
//
int CPosterThread::SendLine(int hSocket, char* p)
{
// TRACE1("Sending: %s", p);
int sl = strlen(p);
return(send(hSocket, p, sl, 0));
}

//
// Call it like this:
// char szLine[1024];
// rv = GetLine(hSocket, szLine, 1023); // max 1023
//
int GetLine(int hSocket, char* p, int MaxChar)
{
char ch = 0;
int n = 0;
int rv;

for(;
{
// Receive one byte..
rv = recv(hSocket, &ch, 1, 0);
// Handle SOCKET_ERROR
if(rv == SOCKET_ERROR) return(0);
// 0 means client gracefully closed connection
if(rv == 0) break;
// 0x0D [\r] is end of line
if(ch == 0x0D) { p[n] = ''\0''; break; }
// Handle backspace [good for interactive use]
if((ch >= 0x20) // (ch == 0x09)) { p[n] = ch; ++n; }
if(n >= MaxChar) { p[MaxChar] = ''\0''; return(MaxChar); }
}
return(n);
}






Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS