Herzlich Willkommen, lieber Gast!
  Sie befinden sich hier:

  Forum » Borland C++ Builder » Communication C++ Server & Java Client

Forum | Hilfe | Team | Links | Impressum | > Suche < | Mitglieder | Registrieren | Einloggen
  Quicklinks: MSDN-Online || STL || clib Reference Grundlagen || Literatur || E-Books || Zubehör || > F.A.Q. < || Downloads   

Autor Thread - Seiten: > 1 <
000
24.03.2007, 17:16 Uhr
Lehoanq



I'm using WindowsXP, wanna create a communication between C++ (C++ Borland - part of BDS2006) and Java. The client (java) will send a text to the server (C++), server shows it. That's all. It seems simple, coz people told me that TCP is OK for this communication. I have been trying for 4 days, but not succeeded yet. There're two problems:

1. On the same computer, they send a line of text successfully. But on different ones, the receiving process on server is invoked 2 times for each sending from client. For example, client sends "Hello", server gets "H" for the first time and later: "ello". Could you tell me why, please? And how to solve it. I gave up build-in TServerSocket (or something like that) in Borland C++ 2006, and moved to winsock32.

2. The accept function for the server to get connection from client makes my GUI (dialog...) not responsible, this function makes the server wait here until there's a client connecting to server. Is it related to "non-blocking" or "blocking" concept? And how to deal with it? I see MFC class (CAsyncSocket) is good at this aspect, but I'm using Borland, so only win32 standard is accepted.

Hope to hear from you geek soon. Thank in advance!

Nice weekend!
 
Profil || Private Message || Suche Download || Zitatantwort || Editieren || Löschen || IP
001
25.03.2007, 00:32 Uhr
Blubber2063



A normal TCP socket is blocking, to avoid the blocking of the GUI, one way ist using non blocking sockets, but the most easy way would be starting a thread for the event handling of the button.

For your first problem, you'll need to show a little bit of your code, normally you should recive data until the connection is cancelled.
 
Profil || Private Message || Suche Download || Zitatantwort || Editieren || Löschen || IP
002
25.03.2007, 19:04 Uhr
~Lehoanq
Gast


Yeah, thanks for your reply. I'm using blocking, but if I add two more lines afterhere, it will be non-blocking, I guess so:

Code:
    unsigned long b = 1;
    ioctlsocket(m_socket,FIONBIO, &b);


Why should we create a thread for GUI rather than a thread for socket? Socket is only one while UI is so many. Could you tell me, please? I don't really understand that.

The first problem, hereafter is the code I use:

Code:
        while(1){
                int nret = 0;    
            nret = recv(m_socket,buffer,100,0);
            if(nret == SOCKET_ERROR) {
               nret = WSAGetLastError();        
               //throw error
            }
            buffer[nret-1] = '\0';
        printf("Recv:%s\n",buffer);
    }


I think it's good enough to get data until the input stream is empty. Please take a look for me. Thanks so much!

Cheers,

Dieser Post wurde am 26.03.2007 um 12:26 Uhr von FloSoft editiert.
 
Profil || Private Message || Suche Download || Zitatantwort || Editieren || Löschen || IP
003
25.03.2007, 20:23 Uhr
Blubber2063



Well i meant that you create a thread for the socket handling if you pressed the button, the GUI runs in as the "main thread", and you create a new one for the event handling(in this case your recive loop.

If i unterstand your problem correctly, the Output you got was
H\n
ello
.
If thats the case your problem is the linefeed at the end of your printf, since Data is send per TCP-packets, it does not neccesarily arrive at the same time, so you get the H, print it, and later you get the ello and print it.
As far as i remember there is a way, hat recv doesn't return until the given number of bytes has been recived, but if you send strings thats not wise, since you don't know length of the strings.
The best way is just to read the strings and send your linefeed from your connection partner.
 
Profil || Private Message || Suche Download || Zitatantwort || Editieren || Löschen || IP
004
26.03.2007, 07:59 Uhr
Lehoanq



Thanks, I create a new separate thread successfully.

More about the receiving process: int first recv, it returns 1 byte, "H", there's no "\n", you know. And then buffer[nret-1] = '\0'; will empty that string, so the output is "" - empty, not null. After that, the second recv, returning 5 bytes "ello\n", then output is "ello";

It's strange, isn't it? What will happend if we send an integer? I will test for more.

Dear Blubber2063, thanks for your help. Have a nice day then!
 
Profil || Private Message || Suche Download || Zitatantwort || Editieren || Löschen || IP
005
26.03.2007, 12:30 Uhr
Blubber2063



Yeah you recived no \n, i just wanted to show that your printf, adds that linefeed when printing the recived message. I would say its normal that you doesn't neccessarily recive the full message with on recv. There are some ways, one is to send an end of message code, so you can decide if the message has been recived completely, or you call recv with
MSG_WAITALL, but the your Buffer should have the correct size.
 
Profil || Private Message || Suche Download || Zitatantwort || Editieren || Löschen || IP
006
26.03.2007, 12:33 Uhr
FloSoft
Medialer Over-Flow
(Administrator)


hi,

you should not decrement the returned bytes, nret-1 is the last received byte.


C++:
buffer[nret] = '\0';



should print

Hello\n

(if the text is sent in one tcp-packet)

With ioctl-calls you can look how much bytes are waiting and receive all of them soon as you think they're enough.

If your client sends always \n-terminated strings, you can read multiple times and print it soon as a \n occures in the read data. You can easily use some kind of ringbuffer for that
--
class God : public ChuckNorris { };

Dieser Post wurde am 26.03.2007 um 12:35 Uhr von FloSoft editiert.
 
Profil || Private Message || Suche Download || Zitatantwort || Editieren || Löschen || IP
007
27.03.2007, 06:11 Uhr
Lehoanq



@Blubber2063: Thank you, I see. And "\n" is and message code of ending

@FloSoft: Yeah, that's right. But on the client side, I added the "\n" (or anything else), and on the server side, I can safely remove it.

I write a function, ReadLine, to repeat "recv 1 byte each time" until reach "\n". That works, at least for my simple application

Thank you all for your kindly attention!

Have a nice day!
 
Profil || Private Message || Suche Download || Zitatantwort || Editieren || Löschen || IP
Seiten: > 1 <     [ Borland C++ Builder ]  


ThWBoard 2.73 FloSoft-Edition
© by Paul Baecher & Felix Gonschorek (www.thwboard.de)

Anpassungen des Forums
© by Flo-Soft (www.flo-soft.de)

Sie sind Besucher: