tal0n
05-21-2003, 10:17 PM
You know me and I know you. You know about this project and I hope you can help. Problem this time is that the client is not recieving the text that another one is sending. I at this point do not care if i get junk or what not. The client will type something and then the prompt for the next line comes up. If someone tries to send text to it then nothing shows up on the screen.
//Client tal0n
#include "tal0n_.h"
#include "list.h"
#include <iostream.h>
#include <stdlib.h>
#define PORT 9090
#define LENGTH 1500
struct sockaddr_in them;
fd_set readfds, writefds, master;
int main(int argc, char *argv[])
{
tal0n_ cl;
char yn;
char buf[LENGTH], rec[LENGTH];
char name[20];
int top;
int fdmax, len;
int ip1,ip2,ip3,ip4;
WSADATA info;
FD_ZERO(&readfds);
FD_ZERO(&writefds);
FD_ZERO(&master);
// Initialize them arrays so I get good values later
cl.init( buf );
cl.init( rec );
// Fix this process up later. Make it friendly for the actual exam
do
{
cout << "\nChoose your IP address:\n"; //It has you enter the server's
cout << "first numeral: "; //IP address one numeral at a time
cin >> ip1;
cout << "second numeral:";
cin >> ip2;
cout << "third numeral: ";
cin >> ip3;
cout << "fourth numeral:";
cin >> ip4;
cout << "\nIs this the server's IP address? [y/n]\n" << ip1 << "." << ip2 << "." << ip3 << "." << ip4 << endl;
cin >> yn; //this just shows the ip and ask for verification
}
while ( yn != 'y' && yn != 'Y' );
cout << "Enter your user name: ";
cin >> name;
if (WSAStartup(MAKEWORD(1, 1), &info) != 0)
{
cerr << "WSAStartup";
return 1;
}
if ( (top = socket(AF_INET, SOCK_STREAM, 0)) == -1 )
{
cerr << "socket";
return 1;
}
them.sin_family=AF_INET;
them.sin_port=htons(PORT);
them.sin_addr.S_un.S_un_b.s_b1 = ip1;
them.sin_addr.S_un.S_un_b.s_b2 = ip2;
them.sin_addr.S_un.S_un_b.s_b3 = ip3;
them.sin_addr.S_un.S_un_b.s_b4 = ip4;
memset( &(them.sin_zero), '\0', 8); // Zero out rest of the structure
if (connect(top, (struct sockaddr *)&them, sizeof(struct sockaddr)) == -1)
{
cerr << "connect";
return 0;
}
FD_SET(top, &master);
fdmax = top+1;
cout << "Sending Screen Name" << endl;
if ( send( top, name, strlen(name), 0) == -1 )
{
cerr << "send";
return 1;
}
cout << "Name Sent" << endl;
//Main Loop
while(1)
{
readfds = master;
writefds = master;
if (select( fdmax, &readfds, &writefds, NULL, NULL) < 0)
{
cerr << "select";
return 1;
}
// Run through the main fun house
for ( int i = 0; i < fdmax; i++ )
{
if (FD_ISSET(top, &readfds))
{
if ( recv(top, rec, sizeof(buf), 0) == -1)
{
cout << "Server disconnected." << endl;
return 0;
}
else
{
for (unsigned int i=0; i < strlen(rec); i++ )
cout << rec[i];
}
}
cout << ": ";
cin.getline( buf, LENGTH, '\n');
len = strlen(buf);
if ( len > LENGTH ) // Prevent the user from entering a message that is to big
cout << "The string is to long" << endl;
else
{
if (FD_ISSET(top, &writefds))
{
if (cl.quitme( buf ) == 1 )
{
if ( send( top, buf, strlen(name), 0) == -1 )
{
cout << "Send upon user exit" << endl;
return 1;
}
closesocket(top);
cout << "Goodbye" << endl;
return 0;
}
else
{
if ( send( top, buf, strlen(name), 0) == -1 )
{
cout << "Send upon no user exit" << endl;
return 1;
}
}
}
}
}
}
closesocket(top);
return 0;
}
The first for loop within the while is not needed but there b/c i thought it might solve something. If you all headers and the server source are needed for you to help me then email me at bluedevils31@triad.rr.com and ask.
//Client tal0n
#include "tal0n_.h"
#include "list.h"
#include <iostream.h>
#include <stdlib.h>
#define PORT 9090
#define LENGTH 1500
struct sockaddr_in them;
fd_set readfds, writefds, master;
int main(int argc, char *argv[])
{
tal0n_ cl;
char yn;
char buf[LENGTH], rec[LENGTH];
char name[20];
int top;
int fdmax, len;
int ip1,ip2,ip3,ip4;
WSADATA info;
FD_ZERO(&readfds);
FD_ZERO(&writefds);
FD_ZERO(&master);
// Initialize them arrays so I get good values later
cl.init( buf );
cl.init( rec );
// Fix this process up later. Make it friendly for the actual exam
do
{
cout << "\nChoose your IP address:\n"; //It has you enter the server's
cout << "first numeral: "; //IP address one numeral at a time
cin >> ip1;
cout << "second numeral:";
cin >> ip2;
cout << "third numeral: ";
cin >> ip3;
cout << "fourth numeral:";
cin >> ip4;
cout << "\nIs this the server's IP address? [y/n]\n" << ip1 << "." << ip2 << "." << ip3 << "." << ip4 << endl;
cin >> yn; //this just shows the ip and ask for verification
}
while ( yn != 'y' && yn != 'Y' );
cout << "Enter your user name: ";
cin >> name;
if (WSAStartup(MAKEWORD(1, 1), &info) != 0)
{
cerr << "WSAStartup";
return 1;
}
if ( (top = socket(AF_INET, SOCK_STREAM, 0)) == -1 )
{
cerr << "socket";
return 1;
}
them.sin_family=AF_INET;
them.sin_port=htons(PORT);
them.sin_addr.S_un.S_un_b.s_b1 = ip1;
them.sin_addr.S_un.S_un_b.s_b2 = ip2;
them.sin_addr.S_un.S_un_b.s_b3 = ip3;
them.sin_addr.S_un.S_un_b.s_b4 = ip4;
memset( &(them.sin_zero), '\0', 8); // Zero out rest of the structure
if (connect(top, (struct sockaddr *)&them, sizeof(struct sockaddr)) == -1)
{
cerr << "connect";
return 0;
}
FD_SET(top, &master);
fdmax = top+1;
cout << "Sending Screen Name" << endl;
if ( send( top, name, strlen(name), 0) == -1 )
{
cerr << "send";
return 1;
}
cout << "Name Sent" << endl;
//Main Loop
while(1)
{
readfds = master;
writefds = master;
if (select( fdmax, &readfds, &writefds, NULL, NULL) < 0)
{
cerr << "select";
return 1;
}
// Run through the main fun house
for ( int i = 0; i < fdmax; i++ )
{
if (FD_ISSET(top, &readfds))
{
if ( recv(top, rec, sizeof(buf), 0) == -1)
{
cout << "Server disconnected." << endl;
return 0;
}
else
{
for (unsigned int i=0; i < strlen(rec); i++ )
cout << rec[i];
}
}
cout << ": ";
cin.getline( buf, LENGTH, '\n');
len = strlen(buf);
if ( len > LENGTH ) // Prevent the user from entering a message that is to big
cout << "The string is to long" << endl;
else
{
if (FD_ISSET(top, &writefds))
{
if (cl.quitme( buf ) == 1 )
{
if ( send( top, buf, strlen(name), 0) == -1 )
{
cout << "Send upon user exit" << endl;
return 1;
}
closesocket(top);
cout << "Goodbye" << endl;
return 0;
}
else
{
if ( send( top, buf, strlen(name), 0) == -1 )
{
cout << "Send upon no user exit" << endl;
return 1;
}
}
}
}
}
}
closesocket(top);
return 0;
}
The first for loop within the while is not needed but there b/c i thought it might solve something. If you all headers and the server source are needed for you to help me then email me at bluedevils31@triad.rr.com and ask.