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.

New Topic/Question
Reply




MultiQuote






|