![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#41 |
|
Highly Adaptive Penguin
Join Date: May 2005
Location: United States
Posts: 249
Rep Power: 4
![]() |
L7sqr, do you have anything on threads yet, the code you emailed to me was very helpful, thanks
|
|
|
|
|
|
#42 |
|
Hobbyist Programmer
Join Date: Jun 2005
Location: here
Posts: 114
Rep Power: 0
![]() |
I decided that putting together something on pthreads in a Linux environment would be counterproductive. Like I said earlier, I'm not all that sure of how things work in Windows (ignorance is bliss, as they say) and thought that it would end up confusing you more.
If you are still looking for something (albiet Linux specific), give me a bit and I might have something by the end of the weekend. [edit] You should note, however, that 2roll4life7 is right, you dont need threads to get this to work (it actualy complicates things). You should use two files (one for client, one for server) instead of trying to ram it all into one file. Follow the layout of the files I provided you. With that program, you open two windows, one for the client, one for the server. Run the server, then the client. What ever you type in the client window is sent to the server, a heading applied and output (in the server window). Running them both in the same window (server in the bg) leads to strange/duplicate output. Try this, detail problems and get back. Good luck.
__________________
"...and though our kids are blessed their parents let them shoulder all the blame." - The Quiet Things That No One Ever Knows [BrandNew] |
|
|
|
|
|
#43 |
|
Highly Adaptive Penguin
Join Date: May 2005
Location: United States
Posts: 249
Rep Power: 4
![]() |
its not all one file. i have 2 seperate programs,im trying to use the select function, so i can send and receive at the same time.
|
|
|
|
|
|
#44 |
|
Hobbyist Programmer
Join Date: Jun 2005
Location: here
Posts: 114
Rep Power: 0
![]() |
To make sure I'm getting this right: You want to set up a 'receiver' that will accept input from a 'sender'. I am assuming that you do not want the 'receiver' to be doing any type of responding (i.e. sending)...correct me if I'm wrong.
__________________
"...and though our kids are blessed their parents let them shoulder all the blame." - The Quiet Things That No One Ever Knows [BrandNew] |
|
|
|
|
|
#45 |
|
Highly Adaptive Penguin
Join Date: May 2005
Location: United States
Posts: 249
Rep Power: 4
![]() |
no, im making an instant messaging program, and i want to be able to send and receive between both programs at the same time, like msn messenger, or aol messenger.
|
|
|
|
|
|
#46 |
|
Hobbyist Programmer
Join Date: Jun 2005
Location: here
Posts: 114
Rep Power: 0
![]() |
Using select:
assuming you have a valid return from accept stroed in cli, you will want to set up a watch on that file descriptor. To do that you create a variable of type fd_set, call it read (since you will be reading from it). Provided for you are macros/functions (they could be either, you dont care) for assigning cli to read. Call in this order: FD_ZERO (&read); /* clear the current contents of read */
FD_SET (cli, &read); /* add cli to the fd_set read */
/* alert select that it should watch for status change in the read fd_set
* the three NULLs indicate:
* no watch on a write fd_set,
* no watch on an exception fd_set,
* no timeout (this is a blocking call)
*/
err = select (cli + 1, &read, NULL, NULL, NULL);
/* this can return errors, check for them */
if (!(FD_ISSET(cli, &read))) {
/* do something appropriate here */
}
/* Ok, so now select let us know that there was a change in one of the fd_sets in read
* and we have checked to see that it is the one we are looking for
*/
/* usually you would do something like:
* nread = recv (cli, &msg, target_read, MSG_WAITALL);
* at this point
*/That is the function of select. Although it can be used with the timeout feature to provide a fairly decent timer function. ![]() In response to your chat program (IM, whatever) - google around for the Windows applications of threads (I provided some links earlier in the post. If you still cant get it, and others here cant make it clear, I'll give a last ditch effort. Try it yourself first though, I think you might suprise yourself.
__________________
"...and though our kids are blessed their parents let them shoulder all the blame." - The Quiet Things That No One Ever Knows [BrandNew] Last edited by L7Sqr; Aug 26th, 2005 at 8:19 PM. |
|
|
|
|
|
#47 |
|
Highly Adaptive Penguin
Join Date: May 2005
Location: United States
Posts: 249
Rep Power: 4
![]() |
yeah thanks. i did what you said and made a multi person chat server.
here's the code //multiperson chat server
//programmed by brent matthews
#include<iostream.h>
#include<winsock.h>
#pragma comment(lib,"wsock32.lib")
#include<string.h>
int main()
{
WORD sock;
WSADATA wsaData;
sock=WSAStartup(0x0101,&wsaData);
fd_set master;
fd_set read_socks;
fd_set write_socks;
sockaddr_in sin;
sockaddr_in dest;
timeval tv;
tv.tv_sec = 30;
tv.tv_usec = 500000;
int sock_max;
int listener;
int new_sock;
char buf[512];
int addrlen;
int i,j;
int nbytes;
int yes=1;
int yeslen=sizeof(yes);
FD_ZERO(&master);
FD_ZERO(&read_socks);
if((listener=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP))==INVALID_SOCKET)
{
cout<<"invalid socket\n";
WSACleanup();
return 0;
}
if(setsockopt(listener, SOL_SOCKET, SO_REUSEADDR, NULL,NULL) == SOCKET_ERROR)
{
cout<<"setsockopt error\n";
}
sin.sin_family=AF_INET;
sin.sin_port=htons(8888);
sin.sin_addr.s_addr=INADDR_ANY;
if(bind(listener,(LPSOCKADDR)&sin,sizeof(sin))==SOCKET_ERROR)
{
cout<<"failed bind\n";
WSACleanup();
return 0;
}
if(listen(listener,10)==SOCKET_ERROR)
{
cout<<"failed listen\n";
WSACleanup();
return 0;
}
FD_SET(listener,&master);
sock_max=listener;
for(;;)
{
read_socks=master;
write_socks=master;
if(select(sock_max+1,&read_socks,NULL,NULL,&tv)==SOCKET_ERROR)
{
cout<<"select timed out\n";
WSACleanup();
return 0;
}
for(i=0;i<=sock_max;i++)
{
if(FD_ISSET(i,&read_socks))
{
if(i==listener)
{
addrlen=sizeof(dest);
if((new_sock=accept(listener,NULL,NULL))==SOCKET_ERROR)
{
cout<<"failed accept\n";
WSACleanup();
return 0;
}
else
{
FD_SET(new_sock,&master);
if(new_sock>sock_max)
{
sock_max=new_sock;
}
cout<<"new connection from: "<<new_sock<<", on: "<<inet_ntoa(dest.sin_addr)<<endl;
}
}
else
{
if((nbytes=recv(i,buf,sizeof(buf),0))<=0)
{
if(nbytes==0)
{
cout<<"socket: "<<i<<" hung up\n";
}
else
{
cout<<"failed recv\n";
WSACleanup();
return 0;
}
closesocket(i);
FD_CLR(i,&master);
}
}
}
else
{
for(j=0;j<=sock_max;j++)
{
if(FD_ISSET(j,&master))
{
if(j!=listener && j!=i)
{
if(send(j,buf,nbytes,0)==SOCKET_ERROR)
{
cout<<"failed send\n";
WSACleanup();
return 0;
}
}
}
}
}
}
}
return 0;
}how is this? and i have one more question: what type of sockets should i use for the client program? |
|
|
|
|
|
#48 |
|
Highly Adaptive Penguin
Join Date: May 2005
Location: United States
Posts: 249
Rep Power: 4
![]() |
could i use select() in my IM instead of threads
|
|
|
|
|
|
#49 |
|
I eat cake for breakfast.
![]() ![]() ![]() ![]() Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9
![]() |
Use iostream and string instead of iostream.h and string.h. Those headers are deprecated - you're not supposed to use them any more. The new headers are fully ANSI-C++-compliant, I believe.
When you do switch, all the functions declared in those headers will be in the std namespace. You can cover for this by either using std::cout, std::cin, std::endl, etc., by typing using std::cout; using std::cin; and so on at the beginning of the source code, or by typing using namespace std; at the beginning of the source. Bear in mind the last one will make everything in iostream, fstream and whatever else you want to use available in the global namespace, so you'll have to name your variables, etc. carefully. |
|
|
|
|
|
#50 |
|
Highly Adaptive Penguin
Join Date: May 2005
Location: United States
Posts: 249
Rep Power: 4
![]() |
those headers do not work with my compiler...i have alraedy tried
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|