c# - Sockets maintain socket open and just check the messages that the client is sending -


what trying check messages client sending. want maintain socket created open , check message client send time time.

i using system.net.sockets only, want make implementation based on barkeley sockets.

here code,

using system; using system.collections.generic; using system.net; using system.net.sockets; using system.text; using system.runtime.remoting.metadata.w3cxsd2001; namespace socket_v1._0 {     class program     {         private const int bufsize = 200;// size of receive buffer         private const int backlog = 5; // o valor backlog define o número de pedidos de conexão que podem ser mantidos em espera sem serem aceites pela "system-call" accept.         static void main(string[] args)         {             int servport = 8070; //port number              socket server = null;             try             {                 // create socket accept client connections                 server = new socket(addressfamily.internetwork, sockettype.stream, protocoltype.tcp);                 server.bind(new ipendpoint(ipaddress.any, servport));                 server.listen(backlog);             }             catch (socketexception se)             {                 console.writeline(se.errorcode + ": " + se.message);                 environment.exit(se.errorcode);             } //for server socket waiting connections             byte[] rcvbuffer = new byte[bufsize]; // definir buffer             int bytesrcvd = 0 ; // numero de bytes recebidos             (; ; )             { // run forever, accepting , servicing connections                 socket client = null;                 try                 {                      client = server.accept(); // client connection                     console.write("handling client @ " + client.remoteendpoint + " - ");                      {                         bytesrcvd = client.receive(rcvbuffer, 0, rcvbuffer.length, socketflags.none);                         string s = encoding.ascii.getstring(rcvbuffer, 0, bytesrcvd);                         }while (true);                 }                 catch (exception e)                 {                     console.writeline(e.message);                     client.close();                 }             }         }       }     } 

so client send http time time, want see messages,

for first time runs string gets http get, , because maintaining socket open, want wait next message (do{}(while(true))). goes client.receive, , stops. after detecting message client enters in forever loop , string empty.

i want similar chat, client send messages , server display message.

so after opening socket, should message client, display , wait message?

one other thing bugging me client http get:

this http get

get /iclock/cdata?sn=2182682370001&options=all&pushver=2.1.2&language=80 http/1.1 host: 192.168.1.43:8070 user-agent: iclock proxy/1.09 connection: close accept: * / * 

what mean when says connection close? need create new socket every time client want send request?

try figure out, did changes on code, inside while have got this

 while ((bytesrcvd = client.receive(rcvbuffer/*, 0, rcvbuffer.length, socketflags.none*/)) > 0)                      {                          totalbytesechoed += bytesrcvd;                         string s = encoding.ascii.getstring(rcvbuffer, 0, bytesrcvd);                         console.writeline(s);                     }  

image: wireshark , console app

so in image have got wireshark running , console app.

according wireshark client had tried send 2 http request, according console displayed one...

i want display 2 calls..

a few years ago, did exercises sockets in c language.

#include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> #include <arpa/inet.h> void main(void) {   struct sockaddr_in me, from;   int newsock, sock = socket(af_inet, sock_stream, 0);   int adl = sizeof(me);   char linha[81];   bzero((char *) &me, adl);   me.sin_family = af_inet;   me.sin_addr.s_addr = htonl(inaddr_any ); /* endereco ip local */   me.sin_port = htons(8450); /* porta local  */   if (-1 == bind(sock, (struct sockaddr *) &me, adl))   {     close(sock);     puts("porta ocupada");     exit(1);   }   listen(sock, 5);   newsock = accept(sock, (struct sockaddr *) &from, &adl);   close(sock);     {     read(newsock, linha, 81);     puts(linha);   } while (strcmp(linha, "exit"));   close(newsock); } 

this in c, can see,after accept socket stays open, , client send message, close socket when user send exit server... there may times client doesnt send...but socket still open, , when detects

this example of want achive, using system.net.sockets library,,,

thanks

there 2 kinds of sockets: socket use listen (it never connected) , sockets correspond connections (each socket represents 1 connection).

accept returns connected socket client accepted. each call accept accepts new , independent client.

if want handle more 1 client @ time (which required) must ensure call accept pending @ times new clients can accepted.

a simple model achieve accept in loop forever , start thread each client accepted:

while (true) {  var clientsocket = listeningsocket.accept();  task.factory.startnew(() => handleclient(clientsocket)); } 

Comments

Popular posts from this blog

javascript - RequestAnimationFrame not working when exiting fullscreen switching space on Safari -

Python ctypes access violation with const pointer arguments -