client-server terminal communication

Hi there, i am looking for an assistance from some one who is familiar to socket programming, or dealt with it before.
I wrote a client-server program and all the methods worked means the socket(), connect(),send(), recv() and close() in the client side, and in the server side also i used the socket(), bind(), listen(), accept(), recv(), send(), and close(). Both terminals showed a positive outcome. What i am looking for is that to type a "Hi" string in client side and to read it in the server terminal. So am looking for your assistance.
Here with the codes of both the client and server programs that i did:


client program
#include <ws2tcpip.h>
#include <iostream>
using namespace std;
int main( void ){
char ip;
unsigned short port;
port = 333;
//int count;

char sendbuf[1024]="";
char recvbuf[100]="";
char Buffer[128];
cout << "CLIENT::\n\n";
cout << "Discovering Winsock version... ";
WSAData webservicesdata; //Declare WSAData
WORD winsockVersion=MAKEWORD(2, 0); //Winsock 2.0
cout << "Winsock version " << winsockVersion << " discovered.\n";
cout << "Starting winsock... ";
int err=WSAStartup(winsockVersion, &webservicesdata);
if (err==-1){
cout << "Error Starting Winsock (v" << winsockVersion <<")\n";}else if (err==0){
cout << "Complete.\n";}


//struct sockaddr_in clientservice;
struct sockaddr_in serverserviceFD;
struct sockaddr_in *res;
//memset(&clientservice, 0, sizeof clientservice);
memset(&serverserviceFD, 0, sizeof serverserviceFD);
serverserviceFD.sin_family = AF_INET;
serverserviceFD.sin_addr.s_addr=inet_addr("127.0.0.1");
serverserviceFD.sin_port=htons(333);



//--------------------------------------------------------------------------

//Create Socket File Descriptor (pointer to where the socket is in memory)
cout << "Creating socket file descriptor... ";
SOCKET clientSocketFD = socket(AF_INET, SOCK_STREAM,IPPROTO_TCP);
if (clientSocketFD!=INVALID_SOCKET){cout<<"Complete.\n";}else{cout<<WSAGetLastError()<<"-ERROR!\n";}


//---------------------------------------------------
//connect() a connection to the socket
cout << "Connecting to the server... ";
SOCKET conerr = connect(clientSocketFD, (sockaddr*)&serverserviceFD, sizeof(serverserviceFD));
if (conerr == -1){
cout << WSAGetLastError() << " - ERROR!\n";
}
else{
cout << "Complete.\n\n";
cout<<"-----------------------------\n";
}

//send() a piece of data sendbuf
cout << "Sending information to server... ";
int sendData = send(clientSocketFD,sendbuf,strlen(sendbuf),0);


if (sendData == -1){
cout << WSAGetLastError() << " - ERROR!\n";
}
else{
cout << "Complete.\n";
}


//recv() a piece of data
cout << "Receiving information... ";
int recvData = recv(clientSocketFD,recvbuf,sizeof(recvbuf),0);
if (recvData == -1){cout << WSAGetLastError() << " - ERROR!\n";}else{cout << "Complete.\n";}



//Close all connections
cout << "Closing connections... ";
shutdown(clientSocketFD,2);
cout << "Complete.\n";

//--------------------------------------------------------------
//Clean up Winsock
cout << "Cleaning Up WinSock... ";
int socketcleanup = WSACleanup();
if(socketcleanup==0){cout<<"Complete.\n";}else{cout<<WSAGetLastError()<<"-ERROR!\n";}
system("PAUSE");return 0;}








server program

#include <ws2tcpip.h>
#include <iostream>
int main( void ){

char sendbuf[2000] = "";
char recvbuf[5000]="";
cout << "SERVER::\n\n";
cout << "Discovering Winsock version... ";
WSAData webservicesdata; //Declare WSAData
WORD winsockVersion=MAKEWORD(2, 0); //Winsock 2.0
cout << "Winsock version " << winsockVersion << " discovered.\n";
cout << "Starting winsock... ";
int err=WSAStartup(winsockVersion, &webservicesdata);
if (err==-1){
cout << "Error Starting Winsock (v" << winsockVersion <<")\n";}else if (err==0){
cout << "Complete.\n";}
struct sockaddr_in serverservice, from;
serverservice.sin_family = AF_INET;
serverservice.sin_addr.s_addr=inet_addr("127.0.0.1");
serverservice.sin_port=htons(333);


//-----------------------------------------------------------------------
//Client addrinfo
struct sockaddr_storage client_addr;
socklen_t client_addr_size;



//------------------------------------------------------------------------------
//Create Socket File Descriptor (pointer to where the socket is in memory)
cout << "Creating socket file descriptor... ";
SOCKET serverserviceFD = socket(PF_INET,SOCK_STREAM,IPPROTO_TCP);
if (serverserviceFD!=INVALID_SOCKET){cout<<"Complete.\n";}else{cout<<WSAGetLastError()<<"-ERROR!\n";}



//-------------------------------------------------------------------
//bind() the Socket to the specified port
cout << "Bind the socket to the specified port";
SOCKET binderr = bind(serverserviceFD, (struct sockaddr*)&serverservice, sizeof(serverservice));
if (binderr==-1){cout<<WSAGetLastError()<<"-ERROR!\n";}else{cout<< "Complete.\n";}




//-----------------------------------------------------------
cout<<"listen() on specified port....";
SOCKET listenErr = listen (serverserviceFD, 10);
if(listenErr==-1||listenErr==INVALID_SOCKET){cout<<WSAGetLastError()<<"-ERROR!\n";}else{cout<< "Complete.\n";}

//-------------------------------------------------------------------------

//accept() a connection on the specified port
cout<<"accept() a connection on the specified port"<<endl;
// client_addr_size = sizeof client_addr;



SOCKET clientSocketFD = accept(serverserviceFD,NULL,NULL);
if (clientSocketFD == -1){cout << WSAGetLastError() << " - ERROR!\n";}else{cout << "Complete.\n";}


//----------------------------------------------
//Entering an infinite loop where we keep trying to receive


cout << "Receiving information... ";

int DataFromClient = SOCKET_ERROR;
while(DataFromClient==SOCKET_ERROR)
{

DataFromClient = recv(clientSocketFD,recvbuf,2000,0);

cout<<recvbuf;
if (DataFromClient==0 || DataFromClient==WSAECONNRESET){cout << WSAGetLastError() << " - ERROR!\n";}else{cout << "Complete.\n";}
}


//Close all connections
cout << "Closing connections... ";

shutdown(clientSocketFD,2);
shutdown(serverserviceFD,2);
cout << "Complete.\n";


//Clean up Winsock
cout << "Cleaning Up WinSock... ";
int socketcleanup = WSACleanup();
if(socketcleanup==0){cout<<"Complete.\n";}else{cout<<WSAGetLastError()<<"-ERROR!\n";}
system("PAUSE");
return 0;
}


so am really waiting for your help.
thanks
Dan Horizon
danielf184@yahoo.com
Topic archived. No new replies allowed.