![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#21 |
|
Highly Adaptive Penguin
Join Date: May 2005
Location: United States
Posts: 249
Rep Power: 4
![]() |
have you finished looking through the code yet DaWei
|
|
|
|
|
|
#22 |
|
Newbie
Join Date: Aug 2005
Posts: 1
Rep Power: 0
![]() |
why are you closing the socket in send_recv function? and why are you trying to listen and accept again?! Once you close the socket, you can no longer receive/send on it. you have to make a new connection to be able to send/receive anything, so keep your socket alive as long as you need it. On the same note, get rid of the connect code in the client send_recv function.
Some general things to follow for a server are: - listen only once for each server you are providing. Dont close the socket only to reopen it again to listen. - accept each connection and deal with it. keep the socket alive as long as you need it. Do not listen/accept on the returned socket from accept. If you are having only one server that does a particular thing, you only need one listen call and one accept call in a loop. Use of select makes sending and receiving easier by not requiring you to wait for the send/recv calls to return. read up on select function. If you want to serve multiple clients using the same server simultaneously, you will have to use either threads or select function. |
|
|
|
|
|
#23 |
|
Highly Adaptive Penguin
Join Date: May 2005
Location: United States
Posts: 249
Rep Power: 4
![]() |
i have read countless tutorials on select but still cant figure it out, could you show me how to use it? thanks
|
|
|
|
|
|
#24 |
|
Programmer
Join Date: Jul 2005
Location: Germany
Posts: 69
Rep Power: 4
![]() |
Hi,
select is not that hard. The basic idea is to issue a nonblocking check whether data iz available on certain sockets. The szenario could be. bind server socket accept connection on request, put the socket into an array or list or whatever cycle through the sequence of sockets checking if you can read if so read it
__________________
-= C makes it easy to shoot yourself in the foot; C++ makes it harder, but when you do succeed, you will blow away your whole leg. =- Bjarne Stroustrup |
|
|
|
|
|
#25 |
|
Highly Adaptive Penguin
Join Date: May 2005
Location: United States
Posts: 249
Rep Power: 4
![]() |
ok...ill give it a shot
|
|
|
|
|
|
#26 |
|
Highly Adaptive Penguin
Join Date: May 2005
Location: United States
Posts: 249
Rep Power: 4
![]() |
hows this:
#include<winsock.h>
#include<windows.h>
#include<iostream.h>
#include<stdlib.h>
#include<stdio.h>
#include<fstream.h>
#pragma comment(lib,"wsock32.lib")
#define MAXBUFLEN 500
//functions
void send_recv();
//global variables
short int port;
int connected=false;
int sent=false;
int received=false;
int client_connected=false;
char rdata[MAXBUFLEN];
char data[500];
char socket_array[4][4];
//socket stuff
SOCKET server;
WORD sockversion;
WSADATA wsaData;
int main(void)
{
if((sockversion=WSAStartup(0x0101,&wsaData))!=SOCKET_ERROR)
{
if((server=socket(PF_INET,SOCK_STREAM,IPPROTO_TCP) )!=INVALID_SOCKET)
{
sockaddr_in from;
int fromlen=sizeof(from);
sockaddr_in sin;
sin.sin_family=AF_INET;
sin.sin_port=htons(80);
sin.sin_addr.s_addr=INADDR_ANY;
if((bind(server,(LPSOCKADDR)&sin,sizeof(sin)))!=SOCKET_ERROR)
{
if((listen(server,4))!=SOCKET_ERROR)
{
SOCKET socket_array[1][1];
if((socket_array[1][1]=accept(server,NULL,NULL))!=SOCKET_ERROR )
{
fd_set ReadFDs, WriteFDs, ExceptFDs;
FD_ZERO(&ReadFDs);
FD_ZERO(&WriteFDs);
FD_ZERO(&ExceptFDs);
if(server != INVALID_SOCKET)
{
FD_SET(server, &ReadFDs);
FD_SET(server, &ExceptFDs);
}
if(select(0, &ReadFDs, &WriteFDs, &ExceptFDs, 0)>0)
{
client_connected=true;
connected=true;
cout<<"\n";
cout<<"-----------------------\n";
cout<<"welcome to Network Chat\n";
cout<<"-----------------------\n";
cout<<"-hlp for help\n";
cout<<"\n";
while(true)
{
char escape[5]="-q";
char ip_addr[5]="-ip";
char clear[5]="-clr";
char help[5]="-hlp";
char disconn[5]="-dis";
char conn[5]="-con";
char status[6]="-stat";
cout<<">> ";
cin.get(data,sizeof(data));
cin.ignore(80,'\n');
if(strcmp(data,escape)==0)
{
WSACleanup();
closesocket(socket_array[4][4]);
return 0;
}
else if(strcmp(data,ip_addr)==0)
{
cout<<"\n";
cout<<"<your IP: "<<inet_ntoa(sin.sin_addr)<<"> "<<'\n';
cout<<"<your port: "<<htons(sin.sin_port)<<">"<<'\n';
cout<<"\n";
cout<<"<server IP: "<<inet_ntoa(from.sin_addr)<<"> "<<'\n';
cout<<"<server port: "<<htons(from.sin_port)<<"> "<<'\n';
cout<<"\n";
}
else if(strcmp(data,clear)==0)
{
system("cls");
}
else if(strcmp(data,help)==0)
{
cout<<"\n";
cout<<"-q quit\n";
cout<<"-clr clear screen\n";
cout<<"-ip server IP and port #\n";
cout<<"-dis disconnect from server\n";
cout<<"-stat connection status( 1 = connected, 0 = not connected)\n";
cout<<"\n";
}
else if(strcmp(data,disconn)==0)
{
cout<<"\n";
closesocket(server);
closesocket(socket_array[4][4]);
WSACleanup();
connected=false;
cout<<"you have disconnected\n";
cout<<"\n";
}
else if(strcmp(data,status)==0)
{
cout<<"\n";
cout<<"<connection status: "<<connected<<" >"<<'\n';
cout<<"\n";
}
else
{
send_recv();
}
}
}
else
{
cout<<"failed select()\n";
WSACleanup();
}
}
else
{
connected=false;
cout<<"failed accept\n";
closesocket(server);
closesocket(socket_array[4][4]);
WSACleanup();
}
}
else
{
connected=false;
cout<<"failed bind\n";
closesocket(server);
closesocket(socket_array[4][4]);
WSACleanup();
}
}
else
{
connected=false;
cout<<"failed bind\n";
closesocket(server);
closesocket(socket_array[4][4]);
WSACleanup();
}
}
else
{
connected=false;
cout<<"invalid socket\n";
WSACleanup();
return 0;
}
}
else
{
cout<<"WSAStartup() failed\n";
WSACleanup();
return 0;
}
}
void send_recv()
{
if((recv(socket_array[4][4],rdata,MAXBUFLEN-1,0))==SOCKET_ERROR)
{
received=false;
int error=WSAGetLastError();
if(error==WSAENOTSOCK)
{
cout<<"not a valid socket\n";
WSACleanup();
}
else if(error==WSAENOTCONN)
{
cout<<"socket not connected\n";
WSACleanup();
}
cout<<"failed recv\n";
WSACleanup();
}
else
{
received=true;
cout<<"client says: "<<rdata<<'\n';
}
if((send(server,data,sizeof(data),0))==SOCKET_ERROR)
{
sent=false;
cout<<"failed send\n";
WSACleanup();
}
else
{
sent=true;
}
} |
|
|
|
|
|
#27 |
|
Hobbyist Programmer
Join Date: Jun 2005
Location: here
Posts: 114
Rep Power: 0
![]() |
If I provided you source code for a simple client/server designed for a linux system would you be able to follow along?
NOTE: it was designed to be an introduction to the topic. NOTE: it is heavily commented, so syntax highlighting may help you The reason I ask is, I am not too familiar with the windows way of things so if it would be a waste, I'd rather not put you through it. However, if you are interested, I will either post here or send you a copy of the source (when I get my sorry arse home from work, that is).
__________________
"...and though our kids are blessed their parents let them shoulder all the blame." - The Quiet Things That No One Ever Knows [BrandNew] |
|
|
|
|
|
#28 |
|
Highly Adaptive Penguin
Join Date: May 2005
Location: United States
Posts: 249
Rep Power: 4
![]() |
yeah that would be great. thanks
|
|
|
|
|
|
#29 |
|
Hobbyist Programmer
Join Date: Jun 2005
Location: here
Posts: 114
Rep Power: 0
![]() |
Ok,
I'll post it up later tonight. Dont hold your breath, though, it might be midnight or later before I can get to my station at home. (currently it 4:30 now). Better yet, pm me an address I can send it to. BTW, do you have access to a linux machine?
__________________
"...and though our kids are blessed their parents let them shoulder all the blame." - The Quiet Things That No One Ever Knows [BrandNew] |
|
|
|
|
|
#30 |
|
Highly Adaptive Penguin
Join Date: May 2005
Location: United States
Posts: 249
Rep Power: 4
![]() |
umm no i dont have access to a linux
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|