2

I have problem in TCP/IP communication between Matlab and C++. I create a server in C++ using MWinsock, that can send and receive data from client. In Matlab (acts as client), I create TCP/IP object, connect to server, and use fprintf to send a text to server [for example, I send text "A005" to server, fprintf(t, 'A005')]. However, on server run in C++ just receive sometime empty text '' or only '05' or 'A0', sometimes is 'A005'. So, could anyone give some help on this problem, many thanks!


Thank you for your answers, The codes are actually simple

*In Matlab, use TCP/IP object:

t = tcpip('IPaddress', 1200); %IPaddress get from server after running 'winServer.exe' file.
fopen(t);  
fprintf(t, 'A005')

*On the server, I use server file developed by LiyangYu (you may download .exe file from here).

I tried and got problem as described above, so could you give any solutions? Thank you!

Shai
  • 111,146
  • 38
  • 238
  • 371
  • 1
    hard to tell without seeing the code. Could it be that output is buffered (i.e you need to flush)? – Amro Apr 21 '13 at 13:33
  • Yeah, let's see the exact code (preferably for both sides). I/O can be fiddly with the details. – Andrew Janke Apr 22 '13 at 02:08
  • 1
    (agree with Amro) Maybe same problem as here : http://stackoverflow.com/questions/6846924/payload-split-over-two-tcp-packets-when-using-boost-asio-when-it-fits-within-th ? Please give more details. – Offirmo Apr 23 '13 at 10:59
  • Please try `fprintf( t, 'A005\n');` to encourage a flush. – Shai Apr 23 '13 at 11:36
  • @XuanThuongTran: just to be clear, are you using the [`tcpip`](http://www.mathworks.com/help/instrument/tcpip.html) object from the Instrument Control toolbox? – Amro Apr 23 '13 at 13:00
  • Hi Armo, that's right, i am using tcpip object from Instrument Control Toolbox. Do you know the reason of my problem? Thanks! – Xuan Thuong Tran Apr 24 '13 at 05:16
  • @Shai: I tried fprintf( t, 'A005\n'); but this time server always receives empty text – Xuan Thuong Tran Apr 24 '13 at 05:23

1 Answers1

0

Did you actually read the code and description of the project you linked to? You can't just pick some random code and expect it to work!

This is an example of a basic TCP chat server (poorly programmed if you ask me). It works with messages formatted according to a specific convention:

  • message length (6 chars right aligned padded with spaces): sprintf('%6d',len)
  • followed by actual message string

With that said, here is a simple client in MATLAB to interact with the C++ chat server:

%# helper functions to send/receive messages according to the protocol
send = @(t,msg) fwrite(t, [sprintf('%6d',length(msg)) msg], 'uint8');
recv = @(t) char(fread(t, str2double(char(fread(t,6,'uint8')')), 'uint8')');

%# connect to server
t = tcpip('127.0.0.1',1200);
fopen(t);

%# send a message
send(t,'hello there')

%# receive response and display it
m = recv(t);
fprintf('[SERVER]: %s\n', m);

%# send BYE message to disconnect from chat
send(t,'bye')

%# close socket and clear it
fclose(t);
delete(t); clear t

Of course the server winserver.exe has to be running first in a separate console (don't forget to type your messages in there to respond to client)

Amro
  • 123,847
  • 25
  • 243
  • 454
  • Actually, I read the code and description from codeproject link but I could not see my problem until now. Now, I found it with your useful helps and comments. As you said, that is the mes_header_len = 6, followed by actual message. Thank you very much! – Xuan Thuong Tran Apr 24 '13 at 16:31