winsock server......agen

wierd output from server

Page 1 of 1

8 Replies - 3537 Views - Last Post: 02 June 2010 - 06:35 PM Rate Topic: -----

#1 UG Cyber  Icon User is offline

  • D.I.C Addict

Reputation: 32
  • View blog
  • Posts: 546
  • Joined: 24-July 08

winsock server......agen

Post icon  Posted 06 December 2008 - 12:11 PM

I am trying to make a simple server to print text that is received so i can recall and debug other clients.....The problem is that i keep getting unreadable ASCII characters repeatedly. So i put a filter on so it wont display the text if the first letter of the last received text matches the current. I created a GUI client to send "Hello" to the correct port and IP but nothing is received.
here is the code for the server
//---------------------------------------------------------------------------

#include <vcl.h>
#include <winsock2.h>
#include <iostream>
#pragma hdrstop
using namespace std;

char last;
//---------------------------------------------------------------------------

#pragma argsused
int main(int argc, char* argv[])
{
        WSADATA wsa;
        SOCKET sock;
        SOCKADDR_IN ServerInfo;
        int port;
        //----------------------
        cout << "Console Server -> UG Cyber" << endl;
        cout << "Press F1 to stop server" << endl << endl;
        cout << "Port Desired:  ";
        cin >> port;
        cout << endl;
        //----------------------
        WSAStartup(0x0202, &wsa);
        ServerInfo.sin_family = AF_INET;
        ServerInfo.sin_port = htons(port);
        ServerInfo.sin_addr.s_addr = inet_addr("0.0.0.0");
        sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
        if (bind(sock, (SOCKADDR*)&ServerInfo, sizeof(ServerInfo)) == INVALID_SOCKET)
        {
                cout << "Could not bind to port " << port << endl;
                return 1;
        }
        listen(sock, 10);
        accept(sock, NULL, NULL);
        while (GetAsyncKeyState(VK_F1) != true)
        {
                char Buffer[1024];
                recv(sock, Buffer, 1024, 0);
                if (Buffer[0] != last)
                {
                        cout << "Client-> " << Buffer << endl;
                        Buffer[0] = last;
                }
        }
        return 0;
}



and here is the console client i also created...
//---------------------------------------------------------------------------

#include <vcl.h>
#include <winsock2.h>
#include <iostream>
#pragma hdrstop
using namespace std;

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

#pragma argsused
int main(int argc, char* argv[])
{
        WSADATA wsa;
        SOCKET sock;
        SOCKADDR_IN Host;
        char IPAddress[1024];
        int Port;
        //-----------------
        cout << "Console TCP client -> UG Cyber" << endl << endl;
        cout << "IP:  ";
        cin >> IPAddress;
        cout << endl << "Port:  ";
        cin >> Port;
        cout << endl;
        //-----------------
        WSAStartup(0x0202, &wsa);
        Host.sin_family = AF_INET;
        Host.sin_port = htons(Port);
        Host.sin_addr.s_addr = inet_addr(IPAddress);
        sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
        if (sock == INVALID_SOCKET)
        {
                cout << "Socket Invalid" << endl;
                return 1;
        }
        if (connect(sock, (SOCKADDR*)&Host, sizeof(Host)) == SOCKET_ERROR)
        {
                cout << "Could not connect to " << IPAddress << endl;
                return 1;
        }
        while (true)
        {
                char Message[1024];
                cout << "Send:  ";
                cin >> Message;
                cout << endl;
                //----------------
                if (send(sock, Message, sizeof(Message), 0) == SOCKET_ERROR)
                {
                        cout << "Fatal error: Could not send message" << endl;
                        cout << "Disconnecting....." << endl;
                        break;
                }
        }
        return 1;

}



I am not getting any errors and it does connect....but wont send/receive anything readable.

Is This A Good Question/Topic? 0
  • +

Replies To: winsock server......agen

#2 UG Cyber  Icon User is offline

  • D.I.C Addict

Reputation: 32
  • View blog
  • Posts: 546
  • Joined: 24-July 08

Re: winsock server......agen

Posted 07 December 2008 - 10:49 AM

bump :^:
Was This Post Helpful? 0
  • +
  • -

#3 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6048
  • View blog
  • Posts: 23,473
  • Joined: 23-August 08

Re: winsock server......agen

Posted 07 December 2008 - 11:55 AM

First thought is always a firewall issue.

You sort of contradict yourself with these two statements:

Quote

nothing is received.
and

Quote

wont send/receive anything readable


Is anything received by the server?

Now, to the code.
while (GetAsyncKeyState(VK_F1) != true)
{
    char Buffer[1024];
    recv(sock, Buffer, 1024, 0);
    if (Buffer[0] != last)
    {
        cout << "Client-> " << Buffer << endl;
        Buffer[0] = last;
    }
}


recv returns a return code. You should be using it.
while (GetAsyncKeyState(VK_F1) != true)
{
    int bytes_received = 0;
    char Buffer[1024];
    bytes_received = recv(sock, Buffer, 1024, 0);
    if (bytes_received == 0)
    {
         // Client disconnected
         break;
     }
     else if (bytes_received == -1)
     {
          // Error
          break;
     }
     // Null-terminate the received data
     Buffer[bytes_received] = 0;
     cout << "Client-> " << Buffer << endl;
}

Was This Post Helpful? 0
  • +
  • -

#4 UG Cyber  Icon User is offline

  • D.I.C Addict

Reputation: 32
  • View blog
  • Posts: 546
  • Joined: 24-July 08

Re: winsock server......agen

Posted 07 December 2008 - 01:27 PM

I did that and i am receiving a -1.
So i placed "WSAGetLastError()" and i am receiving "10057". What does that stand for?
[edit]
Found on google that it stands for "Socket is not connected". However the client and server react when client tries to connect. What could be going wrong??
[edit2] I turned off my firewall to make sure if that was it or not....and it wasnt. its back on now =)

This post has been edited by UG Cyber: 07 December 2008 - 01:44 PM

Was This Post Helpful? 0
  • +
  • -

#5 homemade-jam  Icon User is offline

  • Gabe's Nemesis
  • member icon

Reputation: 11
  • View blog
  • Posts: 1,300
  • Joined: 17-March 08

Re: winsock server......agen

Posted 07 December 2008 - 01:37 PM

I've only had a brief look at this but iirc you need a listen socket and a client socket and I think you only have one socket on the server?
http://msdn.microsof...593(VS.85).aspx
Was This Post Helpful? 0
  • +
  • -

#6 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6048
  • View blog
  • Posts: 23,473
  • Joined: 23-August 08

Re: winsock server......agen

Posted 07 December 2008 - 02:11 PM

Ah yes, h-j's nailed it. accept returns the socket with which you communicate with the client. See the almighty Beej.
Was This Post Helpful? 0
  • +
  • -

#7 UG Cyber  Icon User is offline

  • D.I.C Addict

Reputation: 32
  • View blog
  • Posts: 546
  • Joined: 24-July 08

Re: winsock server......agen

Posted 07 December 2008 - 02:44 PM

Thank you soo much...I love the feeling of the *click of understanding*. But now that raised another question. A While back i created a chat program that worked well, the coding was poor, but it worked.

Here is the link.
http://www.dreaminco...wtopic58831.htm

Now why would that code work but not this one? (That is the code that i used while trying to get this one to work)
Was This Post Helpful? 0
  • +
  • -

#8 Guest_rambo*


Reputation:

Re: winsock server......agen

Posted 02 June 2010 - 06:07 PM

I has try the code above, just wanna try to send a message, but that have problem on my LOG. is any body can help my so i can try using on my peer to peer. thx

this is the code above that i pasted :bigsmile: ..

#include <winsock.h>
#include <windows.h>
#include <iostream>
#define NO_FLAGS_SET 0
#define PORT (u_short) 8000 //44966
#define MAXBUFLEN 256
using namespace std;

int main()
{
WSADATA Data;
    SOCKADDR_IN recvSockAddr;
    SOCKET recvSocket;
    int status;
    int numrcv=0;
    char buffer[MAXBUFLEN];
    memset(buffer,0,MAXBUFLEN);

    /* initialize the Windows Socket DLL */
    status=WSAStartup(MAKEWORD(1, 1), &Data);
    if (status != 0)
    {
        cerr << "ERROR: WSAStartup tidak berhasil" << endl;
        return 0;
    }

    /* zero the sockaddr_in structure */
    memset(&recvSockAddr, 0, sizeof(recvSockAddr));

    /* specify the port portion of the address */
	//PORT TELAH DI DEFINE
    recvSockAddr.sin_port=htons(PORT);

    /* specify the address family as Internet */
    recvSockAddr.sin_family=AF_INET;

    /* specify that the address does not matter */
    recvSockAddr.sin_addr.s_addr= inet_addr( "127.0.0.1" );//65.55.21.250

/* create a socket */
recvSocket=socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (recvSocket == INVALID_SOCKET)
{
    cerr << "ERROR: socket tidak berhasil" << endl;
    system("pause");
    return 0;
}

    //Try to connect
    if ( connect(recvSocket,(SOCKADDR*)&recvSockAddr,sizeof(SOCKADDR_IN)) == SOCKET_ERROR)
    {
        cerr << "Socket gagal koneksi" << endl;
        closesocket(recvSocket);
        WSACleanup();
        return 0;
    }

    //send a request
    send(recvSocket, "GET \r\n\r\n",12,0);

    numrcv=recv(recvSocket, buffer, MAXBUFLEN, NO_FLAGS_SET);
    
    if (numrcv == SOCKET_ERROR)
    {
        cerr << "ERROR: recvfrom tidak berhjasil" << endl;

        status=closesocket(recvSocket);
        if (status == SOCKET_ERROR)
        cerr << "ERROR: closesocket tidak berhasil" << endl;
        status=WSACleanup();
        if (status == SOCKET_ERROR)
        cerr << "ERROR: WSACleanup tidak berhasil" << endl;
        system("pause");
        return(1);
    }

    cout << buffer << endl;
    system("pause");

	return 0;
}



//========================== THE LOG =============
------ Build started: Project: bisa Client_5, Configuration: Release Win32 ------
Linking...
bisa Client_5.obj : error LNK2001: unresolved external symbol _send@16
bisa Client_5.obj : error LNK2001: unresolved external symbol _closesocket@4
bisa Client_5.obj : error LNK2001: unresolved external symbol _socket@12
bisa Client_5.obj : error LNK2001: unresolved external symbol _recv@16
bisa Client_5.obj : error LNK2001: unresolved external symbol _WSACleanup@0
bisa Client_5.obj : error LNK2001: unresolved external symbol _htons@4
bisa Client_5.obj : error LNK2001: unresolved external symbol _inet_addr@4
bisa Client_5.obj : error LNK2001: unresolved external symbol _WSAStartup@8
bisa Client_5.obj : error LNK2001: unresolved external symbol _connect@12
J:\ITB\JARINGAN KOMPUTER\blm bisa Client_5\Release\bisa Client_5.exe : fatal error LNK1120: 9 unresolved externals
Build log was saved at "file://j:\ITB\JARINGAN KOMPUTER\blm bisa Client_5\Client_5\Release\BuildLog.htm"
bisa Client_5 - 10 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

Was This Post Helpful? 0

#9 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6048
  • View blog
  • Posts: 23,473
  • Joined: 23-August 08

Re: winsock server......agen

Posted 02 June 2010 - 06:35 PM

You need to link in the Winsock library. See entry 2.4 here. Or here
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1