• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.

Archived

This topic is now archived and is closed to further replies.

Xeno

Need help with opening port in winsock2

9 posts in this topic

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
0

Share this post


Link to post
Share on other sites

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
0

Share this post


Link to post
Share on other sites
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
0

Share this post


Link to post
Share on other sites
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);
}
0

Share this post


Link to post
Share on other sites
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
0

Share this post


Link to post
Share on other sites
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
0

Share this post


Link to post
Share on other sites
hhmmm.... why it was posted twise, strange .

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

Visit my web site : VSoft
0

Share this post


Link to post
Share on other sites
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
0

Share this post


Link to post
Share on other sites
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
0

Share this post


Link to post
Share on other sites
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);
}

0

Share this post


Link to post
Share on other sites