Wednesday 15 February 2012

Java Communication netty server - Socket client -


I use Natalie as a server for a chat server project, with the following code in my handler: < / P>

  Public Class PacketHandler Channel Expands Inbound Handler Adapter {@ Override Public Wide Channel Reed (Channel Handler Contex CTX, Object Modes) {In Bytube = (Bytefuff) msg; {Try AbstractClientPacket Packet = ClientPacketHandler.handle (in); If (packet! = Null & amp; packet.read ()) {packet.run (); Ctx.write (msg + "\ r \ n"); }} Finally {Reference CountUtil.release (msg); }} @ Override Public Records Exception Capture (Channel Handler Constance CTX, Throwsable) (Reason.printstackscress); Ctx.close (); }}  

So, my packet is handled properly and it works fine, but then, I do ctx.write (msg + "\ r \ n") ; To return the message to my clients, act like a resonance server.

Here is the client's code:

  public class chatclient {static socket socket; Stable data output stream out; In the stable BufferedReader; Public static zero main (string [] args) {try {initSocket (); String test = "salap1"; Test packet packet = new testbook (0x18, test.getBytes ()); SendPacket (Packet); While {true} {try {string message = in.readLine (); Println (message); } Hold (IOException e) {e.printStackTrace (); }} // Test} Catch (IOE Option E) {e.printStackTrace (); } Finally {try {socket.close ()}} Grip (IOException E) {// TODO auto-generated catch block e.printStackTrace (); }}} Private static zero init (socket) {{socket = new socket ("localhost", 58008); Out = New Data Optical Stream (socket.getOutputStream ()); In = new BufferedReader (new InputStreamReader ((socket.getInputStream ()))); } Hold (IOException e) {e.printStackTrace (); }} Private static zero dispatch packet (Test Pocket P) throws IOException {out.write (p.getRawData ()); Out.flush (); The packet has been sent correctly, but I have not got anything in the form of the answer, and when I close my server, the client is due to my code (true) <... due to the weak spamming Is , but I did not get my message back, nothing looks and I do not really know. 

I can not use Neti for the customer because it is for a test purpose, the last customer will be written in C # (Unity Engine), so I can not use it in one, I have to do it with the original socket handling.

Edit:

According to the stars, the packet is sent to the client, but the server is not responding, I do not see the packet in which the server with "Salut 1"

you did:

  ctx.write (msg + " \ R \ n ");  

msg is not a string , but a ByteBuf . If you want to add \ r \ n to the received message, then you should do the following:

  in.writeByte ('\ r'); In.writeByte ('\ n'); (In) ctx.write;  

Also, because you have reused the received message ( in ) as a response, you should not release it:

  / / Do not call it (in) ReferenceCountUtil.release; If you really want to call  ctx.write (msg + "\ r \ n") , please ensure that  StringEncoder  in your pipeline. 


No comments:

Post a Comment