c# - Sending and receiving plain text with TCP -


i want send string through tcp connection:

tr220,2,a10000xx,3545.1743,5119.5794,001.0,1503,52:56:16,2012/09/13,0,0,0,0,0,v,000,0,0,0,,+989123456789,*

i using code send text:

string uri = "http://localhost:1414"; string record = "tr220,2,a10000xx,3545.1743,5119.5794,001.0,1503,52:56:16,2012/09/13,0,0,0,0,0,v,000,0,0,0,,+989123456789,*"; httpwebrequest request = (httpwebrequest) webrequest.create(uri); request.method = "post"; byte[] postbytes = getbytes(record); request.contenttype = "text/plain"; request.contentlength = postbytes.length; stream requeststream = request.getrequeststream(); requeststream.write(postbytes, 0, postbytes.length); 

and getbytes method:

private byte[] getbytes(string str) {     byte[] bytes = new byte[str.length * sizeof(char)];     system.buffer.blockcopy(str.tochararray(), 0, bytes, 0, bytes.length);     return bytes; } 

after sending request, in other side app, string:

post / http/1.1\r\ncontent-type: text/plain\r\nhost: localhost:1414\r\ncontent-length: 212\r\nexpect: 100-continue\r\nconnection: keep-alive\r\n\r\n 

using block of code:

tcplistener = new tcplistener(ipaddress.any, 1414); listenthread = new thread(new threadstart(listenforclients)); listenthread.start(); 

and listenforclients method (some code omitted clarity):

networkstream clientstream = tcpclient.getstream(); byte[] message = new byte[4096]; int bytesread; while (true) {     bytesread = 0;     try { bytesread = clientstream.read(message, 0, 4096); }     catch { break; }     asciiencoding encoder = new asciiencoding();     string data = encoder.getstring(message, 0, bytesread);     messagereceived(data); } 

my question why sent , received strings not same?

are sure know doing? sending http packets raw tcp socket, off course going http protocol strings around true payload. use same kind of sockets on both ends or go insane eventually.

this old seems enough need, always: google friend.

http://www.codeproject.com/articles/12893/tcp-ip-chat-application-using-c

as why tcp socket receiving http connection without hitch? http run on tcp, it's formal protocol on top of it.


Comments

Popular posts from this blog

Perl - how to grep a block of text from a file -

delphi - How to remove all the grips on a coolbar if I have several coolbands? -

javascript - Animating array of divs; only the final element is modified -