c++ windows threads help needed

I am having trouble with a program using threads.

Page 1 of 1

12 Replies - 1732 Views - Last Post: 19 February 2009 - 01:03 PM Rate Topic: -----

#1 lilmike  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 25
  • Joined: 01-February 09

c++ windows threads help needed

Posted 11 February 2009 - 12:57 PM

Hi,
I am having trouble with getting a program to run on my computer when I use threads, which I believe it is necessary to do.


#include <iostream>
#include <winsock2.h>
#include <process.h>
using namespace std;

LPTHREAD_START_ROUTINE startrcv();
LPTHREAD_START_ROUTINE startsnd();
int status, status2;
SOCKET sock;
char buffer[12345];
char info[12345];
HANDLE thread_send;
HANDLE thread_rcv;

int main()
{
SOCKADDR_IN sockinfo;
	WSAData wsaData;
cout << "would you like to connect, or listen, 1 or 2." << endl;
int choice;
cin >> choice;
if(choice==1)
{
cout << "enter the IP address to connect to." << endl;
char ipaddress[20];
cin >> ipaddress;
WSAStartup(0x0202, &wsaData);
sockinfo.sin_family = AF_INET;
sockinfo.sin_port = htons(12345);
sockinfo.sin_addr.s_addr = inet_addr(ipaddress);
sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if(sock == INVALID_SOCKET)
{
cout << "error in socket creation. aborting." << endl;
system("PAUSE");
return 0;
}
if (connect(sock, (sockaddr*)&sockinfo, sizeof(sockinfo)) == SOCKET_ERROR)
{
cout << "unable to connect. aborting." << endl;
system("PAUSE");
return 0;
}
thread_send = CreateThread(NULL, 0, startsnd(), 0, 0, NULL);
thread_rcv = CreateThread(NULL, 0, startrcv(), 0, 0, NULL);
}
else
if(choice==2)
{
WSAStartup(0x0202, &wsaData);
sockinfo.sin_family = AF_INET;
sockinfo.sin_port = htons(12345);
sockinfo.sin_addr.s_addr = inet_addr("0.0.0.0");
sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if(sock == INVALID_SOCKET)
{
cout << "could not make socket, aborting." << endl;
system("PAUSE");
return 0;
}
if(bind(sock, (sockaddr*)&sockinfo, sizeof(sockinfo)) == SOCKET_ERROR)
{
cout << "could not open port, aborting." << endl;
system("PAUSE");
return 0;
}
listen(sock, 10);
accept(sock, NULL, NULL);
thread_send = CreateThread(NULL, 0, startsnd(), 0, 0, NULL);
thread_rcv = CreateThread(NULL, 0, startrcv(), 0, 0, NULL);
}
closesocket(sock);
system("PAUSE");
return 0;
}

LPTHREAD_START_ROUTINE startrcv()
{
while(1)
{
if(info == "/quit")
{
break;
}
status=recv(sock, buffer, 1000000, 0);
if(status == 0 || status == -1)
{
cout << "error in receiving, aborting." << endl;
break;
}
cout << buffer;
}
return 0;
}

LPTHREAD_START_ROUTINE startsnd()
{
while(1)
{
cin >> info;
if(info == "/quit")
{
break;
}
status2=send(sock, info, strlen(info), 0);
if(status2 == -1)
{
cout << "error in sending" << endl;
break;
}
}
return 0;
}



For you who don't know what the program is supposed to do, it is supposed to either listen for a connection, and when it receives one, talk back and forth (chat), or send a connection to one that is already listening.
When I start a server, and then start a connection to the server on 127.0.0.1, then I type something like hi.
It does not send hi to the other end.
Finally, it kills the program, and gives a send or don't send error.
Any help would be appreciated.
-lilmike, aka Michael.

P.S. Sorry for the lack of indenting, I just got into the habit, and didn't indent this program when it was written.

Is This A Good Question/Topic? 0
  • +

Replies To: c++ windows threads help needed

#2 horace  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 290
  • View blog
  • Posts: 1,898
  • Joined: 25-October 06

Re: c++ windows threads help needed

Posted 11 February 2009 - 02:07 PM

the server uses a socket to listen for client connections and when it accepts one accept() returns the socket used for communication with the client, see
http://msdn.microsof...526(VS.85).aspx

this enables the sever to accept multiple clients each of which usualy has its own thread.
therefore the code should be (assume clientSocket is a global variable of type SOCKET)
listen(sock, 10);
clientSocket=accept(sock, NULL, NULL);


your startrcv() and startsnd() functions should use clientSocket for communication

This post has been edited by horace: 11 February 2009 - 02:09 PM

Was This Post Helpful? 0
  • +
  • -

#3 lilmike  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 25
  • Joined: 01-February 09

Re: c++ windows threads help needed

Posted 12 February 2009 - 01:18 PM

Hi,
I have the clientsock now defined, but I think the server is failing to listen.
#include <iostream>
#include <winsock2.h>
#include <process.h>
using namespace std;

LPTHREAD_START_ROUTINE startrcv();
LPTHREAD_START_ROUTINE startsnd();
int status, status2;
SOCKET sock;
SOCKET clientsock;
char buffer[12345];
char info[12345];
HANDLE thread_send;
HANDLE thread_rcv;

int main()
{
SOCKADDR_IN sockinfo;
	WSAData wsaData;
cout << "would you like to connect, or listen, 1 or 2." << endl;
int choice;
cin >> choice;
if(choice==1)
{
cout << "enter the IP address to connect to." << endl;
char ipaddress[20];
cin >> ipaddress;
WSAStartup(0x0202, &wsaData);
sockinfo.sin_family = AF_INET;
sockinfo.sin_port = htons(12345);
sockinfo.sin_addr.s_addr = inet_addr(ipaddress);
clientsock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if(clientsock == INVALID_SOCKET)
{
cout << "error in socket creation. aborting." << endl;
system("PAUSE");
return 0;
}
if (connect(clientsock, (sockaddr*)&sockinfo, sizeof(sockinfo)) == SOCKET_ERROR)
{
cout << "unable to connect. aborting." << endl;
system("PAUSE");
return 0;
}
thread_send = CreateThread(NULL, 0, startsnd(), 0, 0, NULL);
thread_rcv = CreateThread(NULL, 0, startrcv(), 0, 0, NULL);
}
else
if(choice==2)
{
WSAStartup(0x0202, &wsaData);
sockinfo.sin_family = AF_INET;
sockinfo.sin_port = htons(12345);
sockinfo.sin_addr.s_addr = inet_addr("0.0.0.0");
sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
clientsock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if(sock == INVALID_SOCKET || clientsock == INVALID_SOCKET)
{
cout << "could not make socket, aborting." << endl;
system("PAUSE");
return 0;
}
if(bind(sock, (sockaddr*)&sockinfo, sizeof(sockinfo)) == SOCKET_ERROR)
{
cout << "could not open port, aborting." << endl;
system("PAUSE");
return 0;
}
listen(sock, 10);
clientsock = accept(sock, NULL, NULL);
thread_send = CreateThread(NULL, 0, startsnd(), 0, 0, NULL);
thread_rcv = CreateThread(NULL, 0, startrcv(), 0, 0, NULL);
}
closesocket(sock);
closesocket(clientsock);
system("PAUSE");
return 0;
}

LPTHREAD_START_ROUTINE startrcv()
{
while(1)
{
if(info == "/quit")
{
break;
}
status=recv(clientsock, buffer, 1000000, 0);
if(status == 0 || status == -1)
{
cout << "error in receiving, aborting." << endl;
break;
}
cout << buffer;
}
return 0;
}

LPTHREAD_START_ROUTINE startsnd()
{
while(1)
{
cin >> info;
if(info == "/quit")
{
break;
}
status2=send(clientsock, info, strlen(info), 0);
if(status2 == -1)
{
cout << "error in sending" << endl;
break;
}
}
return 0;
}



When I go to connect to 127.0.0.1, it says unable to connect. aborting.
And nothing happens on the server end.
Any help would be appreciated.
-lilmike aka Michael.
Was This Post Helpful? 0
  • +
  • -

#4 horace  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 290
  • View blog
  • Posts: 1,898
  • Joined: 25-October 06

Re: c++ windows threads help needed

Posted 13 February 2009 - 12:15 AM

a few changes to make it work
#include <iostream>

using namespace std;

#include <iostream>
#include <winsock2.h>
#include <process.h>
using namespace std;

DWORD WINAPI  startrcv( LPVOID lpParam);
DWORD WINAPI  startsnd( LPVOID lpParam);

int status, status2;
SOCKET sock;
SOCKET clientsock;
char buffer[12345];
char info[12345];
HANDLE thread_send;
HANDLE thread_rcv;

int main()
{
SOCKADDR_IN sockinfo;
	WSAData wsaData;
cout << "would you like to connect, or listen, 1 or 2." << endl;
int choice;
cin >> choice;
if(choice==1)
{
cout << "enter the IP address to connect to." << endl;
char ipaddress[20];
cin >> ipaddress;
WSAStartup(0x0202, &wsaData);
sockinfo.sin_family = AF_INET;
sockinfo.sin_port = htons(12345);
sockinfo.sin_addr.s_addr = inet_addr(ipaddress);
clientsock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if(clientsock == INVALID_SOCKET)
{
cout << "error in socket creation. aborting." << endl;
system("PAUSE");
return 0;
}
if (connect(clientsock, (sockaddr*)&sockinfo, sizeof(sockinfo)) == SOCKET_ERROR)
{
cout << "unable to connect. aborting." << endl;
system("PAUSE");
return 0;
}
thread_rcv = CreateThread(NULL, 0, startrcv, 0, 0, NULL);
thread_send = CreateThread(NULL, 0, startsnd, 0, 0, NULL);
}
else
if(choice==2)
{
WSAStartup(0x0202, &wsaData);
sockinfo.sin_family = AF_INET;
sockinfo.sin_port = htons(12345);
sockinfo.sin_addr.s_addr = inet_addr("0.0.0.0");
sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
clientsock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if(sock == INVALID_SOCKET || clientsock == INVALID_SOCKET)
{
cout << "could not make socket, aborting." << endl;
system("PAUSE");
return 0;
}
if(bind(sock, (sockaddr*)&sockinfo, sizeof(sockinfo)) == SOCKET_ERROR)
{
cout << "could not open port, aborting." << endl;
system("PAUSE");
return 0;
}
listen(sock, 10);
clientsock = accept(sock, NULL, NULL);
thread_send = CreateThread(NULL, 0, startsnd, 0, 0, NULL);
thread_rcv = CreateThread(NULL, 0, startrcv, 0, 0, NULL);
}
closesocket(sock);
closesocket(clientsock);
system("PAUSE");
return 0;
}

//LPTHREAD_START_ROUTINE startrcv()
DWORD WINAPI  startrcv( LPVOID lpParam)
{
	cout << "receive" << endl;
while(1)
{
if(info == "/quit")
{
break;
}
status=recv(clientsock, buffer, 1000, 0);
if(status == 0 || status == -1)
{
cout << "error in receiving, aborting." << endl;
break;
}
cout << buffer;
}
return 0;
}

DWORD WINAPI  startsnd( LPVOID lpParam)
{
	cout << "send " << endl;
while(1)
{
cin >> info;
if(info == "/quit")
{
break;
}
status2=send(clientsock, info, strlen(info), 0);
if(status2 == -1)
{
cout << "error in sending" << endl;
break;
}
}
return 0;
}


in particular the receiver buffer is too long
status=recv(clientsock, buffer, 1000000, 0);


Was This Post Helpful? 0
  • +
  • -

#5 lilmike  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 25
  • Joined: 01-February 09

Re: c++ windows threads help needed

Posted 13 February 2009 - 01:04 PM

Ok,
It still gives me the could not connect. aborting.
Press any key to continue...

#include <iostream>
#include <winsock2.h>
#include <process.h>
using namespace std;

DWORD WINAPI startrcv(LPVOID lpParam);
DWORD WINAPI startsnd(LPVOID lpParam);
int status, status2;
SOCKET sock;
SOCKET clientsock;
char buffer[12345];
char info[12345];
HANDLE thread_send;
HANDLE thread_rcv;

int main()
{
SOCKADDR_IN sockinfo;
	WSAData wsaData;
cout << "would you like to connect, or listen, 1 or 2." << endl;
int choice;
cin >> choice;
if(choice==1)
{
cout << "enter the IP address to connect to." << endl;
char ipaddress[20];
cin >> ipaddress;
WSAStartup(0x0202, &wsaData);
sockinfo.sin_family = AF_INET;
sockinfo.sin_port = htons(12345);
sockinfo.sin_addr.s_addr = inet_addr(ipaddress);
clientsock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if(clientsock == INVALID_SOCKET)
{
cout << "error in socket creation. aborting." << endl;
system("PAUSE");
return 0;
}
if (connect(clientsock, (sockaddr*)&sockinfo, sizeof(sockinfo)) == SOCKET_ERROR)
{
cout << "unable to connect. aborting." << endl;
system("PAUSE");
return 0;
}
thread_send = CreateThread(NULL, 0, startsnd, 0, 0, NULL);
thread_rcv = CreateThread(NULL, 0, startrcv, 0, 0, NULL);
}
else
if(choice==2)
{
WSAStartup(0x0202, &wsaData);
sockinfo.sin_family = AF_INET;
sockinfo.sin_port = htons(12345);
sockinfo.sin_addr.s_addr = inet_addr("0.0.0.0");
sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
clientsock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if(sock == INVALID_SOCKET || clientsock == INVALID_SOCKET)
{
cout << "could not make socket, aborting." << endl;
system("PAUSE");
return 0;
}
if(bind(sock, (sockaddr*)&sockinfo, sizeof(sockinfo)) == SOCKET_ERROR)
{
cout << "could not open port, aborting." << endl;
system("PAUSE");
return 0;
}
listen(sock, 10);
clientsock = accept(sock, NULL, NULL);
thread_send = CreateThread(NULL, 0, startsnd, 0, 0, NULL);
thread_rcv = CreateThread(NULL, 0, startrcv, 0, 0, NULL);
}
closesocket(sock);
closesocket(clientsock);
system("PAUSE");
return 0;
}

DWORD WINAPI startrcv(LPVOID lpParam)
{
while(1)
{
if(info == "/quit")
{
break;
}
status=recv(clientsock, buffer, 1000, 0);
if(status == 0 || status == -1)
{
cout << "error in receiving, aborting." << endl;
break;
}
cout << buffer;
}
return 0;
}

DWORD WINAPI startsnd(LPVOID lpParam)
{
while(1)
{
cin >> info;
if(info == "/quit")
{
break;
}
status2=send(clientsock, info, strlen(info), 0);
if(status2 == -1)
{
cout << "error in sending" << endl;
break;
}
}
return 0;
}




could it have anything to do with running it on the same computer, or using 127.0.0.1?
Any help would be appreciated.
-lilmike, aka Michael.

P.S. I (hopefully) attached the .cpp file that I am using, just link in ws2_32.a or whatever it is, smile, and compile, maybe it will give you the error too, and you'll be able to help.
P.P.S. sorry if I posted the .cpp file twice.
Was This Post Helpful? 0
  • +
  • -

#6 horace  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 290
  • View blog
  • Posts: 1,898
  • Joined: 25-October 06

Re: c++ windows threads help needed

Posted 13 February 2009 - 01:13 PM

View Postlilmike, on 13 Feb, 2009 - 07:04 PM, said:

could it have anything to do with running it on the same computer, or using 127.0.0.1?
Any help would be appreciated.
-lilmike, aka Michael.

127.0.0.1 is OK it is the localhost

try the cpp code I posted last time that should work - if it does see if you can figure out the difference between it and yours
Was This Post Helpful? 0
  • +
  • -

#7 lilmike  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 25
  • Joined: 01-February 09

Re: c++ windows threads help needed

Posted 14 February 2009 - 10:22 AM

Hi,
I actually changed mine to mirror yours, unless I missed something, but it didn't work.
Still gives me the could not connect, aborting. error
-lilmike, aka Michael.
Was This Post Helpful? 0
  • +
  • -

#8 horace  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 290
  • View blog
  • Posts: 1,898
  • Joined: 25-October 06

Re: c++ windows threads help needed

Posted 14 February 2009 - 11:10 AM

View Postlilmike, on 14 Feb, 2009 - 04:22 PM, said:

Hi,
I actually changed mine to mirror yours, unless I missed something, but it didn't work.
Still gives me the could not connect, aborting. error
-lilmike, aka Michael.

I must have posted a previous version or lost the working one - after creating the threads one has to wait for them to terminate using WaitForMultipleObjects(). Hopefully this is the fixed version
#include <iostream>
#include <winsock2.h>
#include <process.h>
using namespace std;

DWORD WINAPI startrcv(LPVOID lpParam);
DWORD WINAPI startsnd(LPVOID lpParam);
int status, status2;
SOCKET sock;
SOCKET clientsock;
char buffer[12345];
char info[12345];
HANDLE thread_send;
HANDLE thread_rcv;
HANDLE threads[2];

int main()
{
SOCKADDR_IN sockinfo;
	WSAData wsaData;
cout << "would you like to connect, or listen, 1 or 2." << endl;
int choice;
cin >> choice;
if(choice==1)
{
cout << "enter the IP address to connect to." << endl;
char ipaddress[20];
cin >> ipaddress;
WSAStartup(0x0202, &wsaData);
sockinfo.sin_family = AF_INET;
sockinfo.sin_port = htons(12345);
sockinfo.sin_addr.s_addr = inet_addr(ipaddress);
clientsock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if(clientsock == INVALID_SOCKET)
{
cout << "error in socket creation. aborting." << endl;
system("PAUSE");
return 0;
}
if (connect(clientsock, (sockaddr*)&sockinfo, sizeof(sockinfo)) == SOCKET_ERROR)
{
cout << "unable to connect. aborting." << endl;
system("PAUSE");
return 0;
}
	   threads[1] = CreateThread(NULL, 0, startsnd, 0, 0, NULL);
		threads[0] = CreateThread(NULL, 0, startrcv, 0, 0, NULL);
		WaitForMultipleObjects(2, threads, TRUE, INFINITE);
}
else
if(choice==2)
{
WSAStartup(0x0202, &wsaData);
sockinfo.sin_family = AF_INET;
sockinfo.sin_port = htons(12345);
sockinfo.sin_addr.s_addr = inet_addr("0.0.0.0");
sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
clientsock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if(sock == INVALID_SOCKET || clientsock == INVALID_SOCKET)
{
cout << "could not make socket, aborting." << endl;
system("PAUSE");
return 0;
}
if(bind(sock, (sockaddr*)&sockinfo, sizeof(sockinfo)) == SOCKET_ERROR)
{
cout << "could not open port, aborting." << endl;
system("PAUSE");
return 0;
}
listen(sock, 10);
clientsock = accept(sock, NULL, NULL);
		threads[1] = CreateThread(NULL, 0, startsnd, 0, 0, NULL);
		threads[0] = CreateThread(NULL, 0, startrcv, 0, 0, NULL);
		WaitForMultipleObjects(2, threads, TRUE, INFINITE);
}
closesocket(sock);
closesocket(clientsock);
system("PAUSE");
return 0;
}

DWORD WINAPI startrcv(LPVOID lpParam)
{
while(1)
{
if(info == "/quit")
{
break;
}
memset(buffer, 0, sizeof(buffer));
status=recv(clientsock, buffer, 1000, 0);
if(status == 0 || status == -1)
{
cout << "error in receiving, aborting." << endl;
break;
}
cout << buffer;
}
return 0;
}

DWORD WINAPI startsnd(LPVOID lpParam)
{
while(1)
{
cin >> info;
if(info == "/quit")
{
break;
}
status2=send(clientsock, info, strlen(info), 0);
if(status2 == -1)
{
cout << "error in sending" << endl;
break;
}
}
return 0;
}


see
http://msdn.microsof...025(VS.85).aspx
Was This Post Helpful? 0
  • +
  • -

#9 lilmike  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 25
  • Joined: 01-February 09

Re: c++ windows threads help needed

Posted 17 February 2009 - 12:59 PM

Ok, I seem to have this code also the same as yours.
I thank you for reposting it, as it obviously adds some functionality, or needed calls, that mine was lacking.
Unfortunately, it still gives me the could not connect error.
This makes me think that it is not listening for connections properly.
I added a test call to see if there was an error, but it didn't seem to have one.

#include <iostream>
#include <winsock2.h>
#include <process.h>
using namespace std;

DWORD WINAPI startrcv(LPVOID lpParam);
DWORD WINAPI startsnd(LPVOID lpParam);
int status, status2;
SOCKET sock;
SOCKET clientsock;
char buffer[12345];
char info[12345];
HANDLE thread_send;
HANDLE thread_rcv;;
HANDLE threads[2];

int main()
{
SOCKADDR_IN sockinfo;
	WSAData wsaData;
cout << "would you like to connect, or listen, 1 or 2." << endl;
int choice;
cin >> choice;
if(choice==1)
{
cout << "enter the IP address to connect to." << endl;
char ipaddress[20];
cin >> ipaddress;
WSAStartup(0x0202, &wsaData);
sockinfo.sin_family = AF_INET;
sockinfo.sin_port = htons(12345);
sockinfo.sin_addr.s_addr = inet_addr(ipaddress);
clientsock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if(clientsock == INVALID_SOCKET)
{
cout << "error in socket creation. aborting." << endl;
system("PAUSE");
return 0;
}
if (connect(clientsock, (sockaddr*)&sockinfo, sizeof(sockinfo)) == SOCKET_ERROR)
{
cout << "unable to connect. aborting." << endl;
system("PAUSE");
return 0;
}
threads[1] = CreateThread(NULL, 0, startsnd, 0, 0, NULL);
threads[0] = CreateThread(NULL, 0, startrcv, 0, 0, NULL);
WaitForMultipleObjects(2, threads, TRUE, INFINITE);
}
else
if(choice==2)
{
WSAStartup(0x0202, &wsaData);
sockinfo.sin_family = AF_INET;
sockinfo.sin_port = htons(12345);
sockinfo.sin_addr.s_addr = inet_addr("0.0.0.0");
sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
clientsock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if(sock == INVALID_SOCKET || clientsock == INVALID_SOCKET)
{
cout << "could not make socket, aborting." << endl;
system("PAUSE");
return 0;
}
if(bind(sock, (sockaddr*)&sockinfo, sizeof(sockinfo)) == SOCKET_ERROR)
{
cout << "could not open port, aborting." << endl;
system("PAUSE");
return 0;
}
int ltest;
ltest = listen(sock, 10);
if(ltest==-1)
{
cout << "could not listen. aborting." << endl;
system("PAUSE");
return 0;
}
clientsock = accept(sock, NULL, NULL);
threads[1] = CreateThread(NULL, 0, startsnd, 0, 0, NULL);
threads[0] = CreateThread(NULL, 0, startrcv, 0, 0, NULL);
WaitForMultipleObjects(2, threads, TRUE, INFINITE);
}
closesocket(sock);
closesocket(clientsock);
system("PAUSE");
return 0;
}

DWORD WINAPI startrcv(LPVOID lpParam)
{
while(1)
{
if(info == "/quit")
{
break;
}
memset(buffer, 0, sizeof(buffer));
status=recv(clientsock, buffer, 1000, 0);
if(status == 0 || status == -1)
{
cout << "error in receiving, aborting." << endl;
break;
}
cout << buffer;
}
return 0;
}

DWORD WINAPI startsnd(LPVOID lpParam)
{
while(1)
{
cin >> info;
if(info == "/quit")
{
break;
}
status2=send(clientsock, info, strlen(info), 0);
if(status2 == -1)
{
cout << "error in sending" << endl;
break;
}
}
return 0;
}



Again, any help would be very deeply appreciated.
Also, I believe, although I am not sure, that I have only run this program on one computer, I will try to run it on another and see what happens.

-lilmike, aka Michael.
Was This Post Helpful? 0
  • +
  • -

#10 horace  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 290
  • View blog
  • Posts: 1,898
  • Joined: 25-October 06

Re: c++ windows threads help needed

Posted 17 February 2009 - 01:10 PM

View Postlilmike, on 17 Feb, 2009 - 06:59 PM, said:

Ok, I seem to have this code also the same as yours.
I thank you for reposting it, as it obviously adds some functionality, or needed calls, that mine was lacking.
Unfortunately, it still gives me the could not connect error.
This makes me think that it is not listening for connections properly.
I added a test call to see if there was an error, but it didn't seem to have one.

Again, any help would be very deeply appreciated.
Also, I believe, although I am not sure, that I have only run this program on one computer, I will try to run it on another and see what happens.

-lilmike, aka Michael.

your code works OK on my machine using the gcc compiler with libws2_32.a
I assume you use the localhost address 127.0.0.1
Was This Post Helpful? 0
  • +
  • -

#11 Plus  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 41
  • View blog
  • Posts: 414
  • Joined: 24-November 08

Re: c++ windows threads help needed

Posted 17 February 2009 - 01:33 PM

:: i haven't studied these methods but,
:: what i know is that this function returns an error,
:: and that's why this condition return true ...

if (connect(clientsock, (sockaddr*)&sockinfo, sizeof(sockinfo)) == SOCKET_ERROR){
cout << "unable to connect. aborting." << endl;
system("PAUSE");
return 0;
}



:: i think that conversion to a pointer of a complex type
:: is something that need more than a basic cast,

:: try this ...

if (connect(clientsock,dynamic_cast<sockaddr*>(&sockinfo), sizeof(sockinfo)) == SOCKET_ERROR){
cout << "unable to connect. aborting." << endl;
system("PAUSE");
return 0;
}



:: if this doesn't work too, then check clientsock, because it might
:: be the problem ...
Was This Post Helpful? 0
  • +
  • -

#12 lilmike  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 25
  • Joined: 01-February 09

Re: c++ windows threads help needed

Posted 18 February 2009 - 10:21 AM

Ok,
I tried to dynamic_cast it, but it said that dynamic_cast was undeclared, so I think you forgot to tell me something I needed to include, or a library to link in.
Code is the same as before, as I could not compile with dynamic_cast.
I will try to remember to try the program on my computer at home, and I will update you when I do.
-lilmike, aka Michael.
Was This Post Helpful? 0
  • +
  • -

#13 lilmike  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 25
  • Joined: 01-February 09

Re: c++ windows threads help needed

Posted 19 February 2009 - 01:03 PM

Ok,
An update:
I got the program to work on my computer at home.
This laptop does not seem to want to run the program, for reasons that may remain unknown.
Now I have a different problem:
That of the fact that instead of shutting down the program when you type /quit, it sends /quit to the other connection.
Any help would, as alwasy, be appreciated.

#include <iostream>
#include <winsock2.h>
#include <process.h>
using namespace std;

DWORD WINAPI startrcv(LPVOID lpParam);
DWORD WINAPI startsnd(LPVOID lpParam);
int status, status2;
SOCKET sock;
SOCKET clientsock;
char buffer[12345];
char info[12345];
HANDLE thread_send;
HANDLE thread_rcv;;
HANDLE threads[2];

int main()
{
SOCKADDR_IN sockinfo;
	WSAData wsaData;
cout << "would you like to connect, or listen, 1 or 2." << endl;
int choice;
cin >> choice;
if(choice==1)
{
cout << "enter the IP address to connect to." << endl;
char ipaddress[20];
cin >> ipaddress;
WSAStartup(0x0202, &wsaData);
sockinfo.sin_family = AF_INET;
sockinfo.sin_port = htons(12345);
sockinfo.sin_addr.s_addr = inet_addr(ipaddress);
clientsock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if(clientsock == INVALID_SOCKET)
{
cout << "error in socket creation. aborting." << endl;
system("PAUSE");
return 0;
}
if (connect(clientsock, (sockaddr*)&sockinfo, sizeof(sockinfo)) == SOCKET_ERROR)
{
cout << "unable to connect. aborting." << endl;
system("PAUSE");
return 0;
}
threads[1] = CreateThread(NULL, 0, startsnd, 0, 0, NULL);
threads[0] = CreateThread(NULL, 0, startrcv, 0, 0, NULL);
WaitForMultipleObjects(2, threads, TRUE, INFINITE);
}
else
if(choice==2)
{
WSAStartup(0x0202, &wsaData);
sockinfo.sin_family = AF_INET;
sockinfo.sin_port = htons(12345);
sockinfo.sin_addr.s_addr = inet_addr("0.0.0.0");
sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
clientsock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if(sock == INVALID_SOCKET || clientsock == INVALID_SOCKET)
{
cout << "could not make socket, aborting." << endl;
system("PAUSE");
return 0;
}
if(bind(sock, (sockaddr*)&sockinfo, sizeof(sockinfo)) == SOCKET_ERROR)
{
cout << "could not open port, aborting." << endl;
system("PAUSE");
return 0;
}
int ltest;
ltest = listen(sock, 10);
if(ltest==-1)
{
cout << "could not listen. aborting." << endl;
system("PAUSE");
return 0;
}
clientsock = accept(sock, NULL, NULL);
threads[1] = CreateThread(NULL, 0, startsnd, 0, 0, NULL);
threads[0] = CreateThread(NULL, 0, startrcv, 0, 0, NULL);
WaitForMultipleObjects(2, threads, TRUE, INFINITE);
}
closesocket(sock);
closesocket(clientsock);
system("PAUSE");
return 0;
}

DWORD WINAPI startrcv(LPVOID lpParam)
{
while(1)
{
if(info == "/quit")
{
break;
}
memset(buffer, 0, sizeof(buffer));
status=recv(clientsock, buffer, 1000, 0);
if(status == 0 || status == -1)
{
cout << "error in receiving, aborting." << endl;
break;
}
cout << buffer << endl;
}
return 0;
}

DWORD WINAPI startsnd(LPVOID lpParam)
{
while(1)
{
cin >> info;
if(info == "/quit")
{
break;
}
status2=send(clientsock, info, strlen(info), 0);
if(status2 == -1)
{
cout << "error in sending" << endl;
break;
}
}
return 0;
}




Also, I will hopefully make it so that when the connection is dropped on the receiving end, it shuts down the whole program, instead of waiting for input.
-lilmike, aka Michael.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1