|
-
February 17th, 2009, 04:12 PM
#1
[winsock] Could someone look over this?
It compiles fine, though I'm not sure if I'm doing everything right.
All I have is the server right now. It's a blocking socket, though it uses select, so it doesn't really block.
Code:
#include <winsock.h>
#include <iostream>
#include <vector>
// using
using std::cout;
using std::vector;
// var
WSADATA wsaData;
SOCKET SOCK;
vector<SOCKET> SOCKS;
sockaddr_in LOCAL;
fd_set acceptfds, readfds, sendfds;
timeval timev;
vector<char> CLIENTBUF;
// proto
void RunServer();
void Accept();
void AcceptClient();
void SendData();
void RecvData();
void Remove(int slot);
int main()
{
int err_WSAStartup = WSAStartup(MAKEWORD(2,2), &wsaData);
if(err_WSAStartup != 0)
{
cout << "WSAStartup() Failed.\n";
Sleep(1000);
exit(-1);
}
LOCAL.sin_family = AF_INET;
LOCAL.sin_addr.s_addr = INADDR_ANY;
LOCAL.sin_port = htons((u_short)55555);
memset(LOCAL.sin_zero, 0, sizeof(LOCAL.sin_zero));
SOCK = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if(SOCK == INVALID_SOCKET)
{
cout << "socket() Failed.\n";
Sleep(1000);
exit(-1);
}
int err_bind = bind(SOCK, (LPSOCKADDR)&LOCAL, sizeof(LOCAL));
if(err_bind != 0)
{
cout << "bind() Failed.\n";
Sleep(1000);
exit(-1);
}
int err_listen = listen(SOCK, 1);
if(err_listen != 0)
{
cout << "listen() Failed.\n";
Sleep(1000);
exit(-1);
}
timev.tv_sec = 0;
timev.tv_usec = 0;
SOCKS.resize(9);
CLIENTBUF.resize(9);
cout << "Server created and waiting for connections.\n";
while(true)
{
RunServer();
}
return 0;
}
void RunServer()
{
// Accept clients
Accept();
SendData();
RecvData();
Sleep(500);
}
void Accept()
{
FD_ZERO(&acceptfds);
FD_SET(SOCK, &acceptfds);
int res = select(0, &acceptfds, NULL, NULL, &timev);
if(res > 0)
{
if(FD_ISSET(SOCK, &acceptfds))
{
AcceptClient();
}
}
}
void AcceptClient()
{
for(unsigned int i=0;i<SOCKS.size();i++)
{
if(SOCKS[i] == NULL)
{
SOCKS[i] = accept(SOCK, NULL, NULL);
cout << "Client accepted on slot: " << (int)i << "\n";
i = 2;
}
}
}
void RecvData()
{
FD_ZERO(&readfds);
for(unsigned int i=0;i<SOCKS.size();i++)
{
if(SOCKS[i] != NULL)
{
FD_SET(SOCKS[i], &readfds);
}
}
int res = select(0, &readfds, NULL, NULL, &timev);
if(res > 0)
{
for(unsigned int j=0;j<SOCKS.size();j++)
{
if(SOCKS[j] != NULL)
{
if(FD_ISSET(SOCKS[j], &readfds))
{
int recvb = recv(SOCKS[j], &CLIENTBUF[j], 100, 0);
if(recvb > 0)
{
cout << "Slot[" << (int)j << "]->Message: "
<< CLIENTBUF[j] << "\n";
}
else if(recvb == -1)
{
Remove((int)j);
}
}
}
}
}
}
void SendData()
{
FD_ZERO(&sendfds);
for(unsigned int i=0;i<SOCKS.size();i++)
{
if(SOCKS[i] != NULL)
{
FD_SET(SOCKS[i], &sendfds);
}
}
int res = select(0, NULL, &sendfds, NULL, &timev);
if(res > 0)
{
for(unsigned int j=0;j<SOCKS.size();j++)
{
if(SOCKS[j] != NULL)
{
if(FD_ISSET(SOCKS[j], &sendfds))
{
int sendb = send(SOCKS[j], &CLIENTBUF[j], 100, 0);
if(sendb > 0)
{
cout << "Sent to slot: " << (int)j << "\n";
}
else if(sendb == -1)
{
Remove((int)j);
}
}
}
}
}
}
void Remove(int slot)
{
if(SOCKS[slot] != NULL)
{
closesocket(SOCKS[slot]);
cout << "Client remove from slot: "
<< slot << "\n";
}
else
{
cout << "Slot " << slot << "is NULL.\n"
<< " Client not removed.\n";
}
}
If someone could please look over that and see if there is something(s) not right.
Also I once heard that on a send or recv call not all data is sent in one call if it's to large or something.
So when I tried this out once with a client I had created a few days ago I noticed that only one char was sent, So I guess that would be one byte.
Like if I sent the word "hello" I would only get "h".
Do I need to change how I send and recv?
Thanks.
-
February 18th, 2009, 12:01 AM
#2
Re: [winsock] Could someone look over this?
Well never mind guys. Ive decided to go multi threaded.
1) Threads are easy to create.
2) Doing multiple connections with select get real complicated with just simple server features.
3) Probably runs smoother.
Thanks,
Clipster
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|