Send packet from server to a specific client using TCP c# -
i writing client-server application. client sends udp broadcast message out trying locate server, server sends udp broadcast message out identifying location.
when client receives message identifying servers location attempts connect server using socket.connect(remoteendpoint).
the server listens these tcp requests on listening socket , accepts requests using listensocket.accept(). clients details stored in array (including ip , port number)
the client sends message server information username , password user entered.
the server checks database username , password , depending on result sends tcp message (this breaks) client sent request check u&p using listening socket. trying use below code send tcp message not work error.
tcpsocket.sendto(message, clientsarray[i].theirsocket.remoteendpoint);
i'm not sure method using.
but in c# there 2 common classes can use server : tcpclient & socket
in tcpclient
... //start server int32 port = 13000; ipaddress localaddr = ipaddress.parse("127.0.0.1"); server = new tcplistener(localaddr, port); server.start(); //accept client tcpclient client = server.accepttcpclient(); networkstream stream = client.getstream(); string data = "message server"; byte[] msg = system.text.encoding.ascii.getbytes(data); //send client stream.write(msg, 0, msg.length); ... and in socket using tcp
... //start server socket listensocket = new socket(addressfamily.internetwork, sockettype.stream, protocoltype.tcp); ipaddress hostip = (dns.resolve(ipaddress.any.tostring())).addresslist[0]; ipendpoint ep = new ipendpoint(hostip, port); listensocket.bind(ep); listensocket.listen(backlog); //accept client socket handler = listener.accept(); string data = "message server"; byte[] msg = encoding.ascii.getbytes(data); //send client handler.send(msg); ... and in socket using udp
you shouldn't use on server since using tcp
... iphostentry hostentry = dns.gethostentry(dns.gethostname()); ipendpoint endpoint = new ipendpoint(hostentry.addresslist[0], 11000); socket s = new socket(endpoint.address.addressfamily, sockettype.dgram, protocoltype.udp); string data = "message server"; byte[] msg = encoding.ascii.getbytes(data); //send client udp s.sendto(msg, endpoint); ...
Comments
Post a Comment