C# Socket Receive to String -


following this guide wrote following function:

public string connecttohost(string ip, int port) {     socket socket = new socket(sockettype.stream, protocoltype.tcp);      ipaddress host = ipaddress.parse(ip);     ipendpoint ipep = new ipendpoint(host, port);      socket.connect(ipep);      byte[] msg = encoding.unicode.getbytes("<client quit>");     int msgsend = socket.send(msg);      byte[] bytes;     int byterecieve;      string msgrecieved = encoding.unicode.getstring(bytes, 0, byterecieve);     while (socket.available > 0)     {         byterecieve = socket.receive(bytes);         msgrecieved += encoding.unicode.getstring(bytes, 0, byterecieve);     }      socket.shutdown(socketshutdown.both);     socket.close();      return msgrecieved; } 

but error

use of unassigned local variable 'bytes' , use of unassigned local variable 'byterecieve'

on lie of code

string msgrecieved = encoding.unicode.getstring(bytes, 0, byterecieve); 

removing encoding.unicode.getstring(bytes, 0, byterecieve); line , have string msgrecieved; gave same error within while loop.

how function return recieved bytes string?

well, it's true using variables without them being assigned, no? that's not allowed because it's unclear means. in case can make work saying:

    byte[] bytes = new byte[4096];     int byterecieve = 0; 

when have solved compiler errors, have nothing sockets, you'll find using available underestimates amount of data that's incoming. never useful.

also, you'll find unicode encoded strings cannot broken @ arbitrary boundaries not work either. use streamreader.

you should keep looking different tutorial. broken. tcp hard right.


Comments

Popular posts from this blog

php - Wordpress website dashboard page or post editor content is not showing but front end data is showing properly -

javascript - Get parameter of GET request -

javascript - Twitter Bootstrap - how to add some more margin between tooltip popup and element -