#include "sock.h"
const char* MSG1 = "Hello MSG1";
const char* MSG3 = "Hello MSG3";
int main(int argc, char* argv[])
{
if (argc < 2)
{
cerr << "Usage: " << argv[0] << " <sockname|port> [<host>]" << endl;
return 11;
}
int port = -1, rc;
(void)sscanf(argv[1], "%d", &port);
char buf[80], *host = ((port == -1) ? argv[1] : argv[2]), socknm[80];
cout << "C: stage 1\n";
sock sp( port != -1 ? AF_INET : AF_UNIX, SOCK_STREAM );
cout << "C: stage 2\n";
if (!sp.good()) return 12;
cout << "C: stage 3: connect\n";
if (sp.connect(host, port) < 0) return 13;
cout << "***************Start************\n";
/* cout << "C: stage 4: send 1\n";
if (sp.write(MSG1, strlen(MSG1) + 1) < 0) return 14;
cout << "C: stage 5: recive 1\n";
if (rc=sp.read(buf, sizeof(buf)) < 0) return 15;
cerr << "client: receive msg: '" << buf << "'\n";
*/
string str;
if (rc=sp.read(buf, sizeof(buf)) < 0) return 5;
cout << "Server: " << buf << "\n";
cin >> str;
if (sp.write(str.c_str(), strlen(str.c_str()) + 1) < 0) return 6;
cout << endl;
if (rc=sp.read(buf, sizeof(buf)) < 0) return 5;
cout << "Server: " << buf << "\n";
cin >> str;
if (sp.write(str.c_str(), strlen(str.c_str()) + 1) < 0) return 6;
cout << endl;
/*
cout << "C: stage 6: send 2\n";
if ((rc = sp.writeto(MSG3, strlen(MSG3) + 1, 0, host, port, -1)) < 0) return 16;
cout << "C: stage 7: recive 2\n";
if ((rc = read(sp.fd(), buf, sizeof(buf))) == -1) return 17;
cerr << "client: read msg '" << buf << "'\n";
cout << "C: stage 8: shutdown\n";*/
// sp.shutdown();
return 0;
}