sockets - Java Client to Server Unknown Source -
i have simple pong game needs work on network, server create game positions of ball , 2 bats, when client connects server, server create new class known pongplayerthread deal input , output streams of client server,
my server works 100% fine without info client server, server can send info client without problems, have unusual problem, first here code, can see have.
pongserver
try { serversocket = new serversocket(port); hear = true; system.out.println("server setup , seek create socket"); } catch(ioexception e) { system.err.println("could not hear on port:" + port); system.exit(1); } while(listen) { players[idplayer] = new pongplayerthread(serversocket.accept(), idplayer, rtninfo()); players[idplayer].start(); system.out.println("client connected id:" + idplayer); players[0].passdata(rtninfo()); idplayer++; if(idplayer > 1) { hear = false; playing = true; } } while(playing) { players[0].passdata(rtninfo()); players[0].sleep(25); players[1].passdata(rtninfo()); players[1].sleep(25); } ....//more, not of import
here pongclient
try { socket = new socket(host, port); serverout = new printwriter(socket.getoutputstream(), true); serverinput = new bufferedreader(new inputstreamreader(socket.getinputstream())); } grab (unknownhostexception e) { system.err.println("couold not connect host:" + host); system.exit(1); } grab (ioexception e) { system.err.println("could not input/output server"); system.exit(1); } ... while ((pos = serverinput.readline()) != null) { string text = "nothing"; serverout.println(text); string[] posvalues = pos.split(":"); model.getball().setx(double.parsedouble(posvalues[0])); model.getball().sety(double.parsedouble(posvalues[1])); /*if(pongcontroller.moveup == true) { system.out.println("up"); serverout.println("up"); pongcontroller.moveup = false; } else { serverout.println("nothing"); }*/ }
here pongplayerthread
try { printwriter out = new printwriter(socket.getoutputstream(), true); bufferedreader in = new bufferedreader( new inputstreamreader( socket.getinputstream())); string text = "hhh"; system.out.println(in.tostring()); //system.out.println(text = in.readline()); system.out.println("checking readline value"); string line; if ((line = in.readline()) == null) { system.out.println("a ok"); } else { system.out.println(":" + line); } while(send) { //string temp = in.readline(); //if(temp.equals("up")) //{ // system.out.println("up say"); //} out.println(pongdata); } out.close(); in.close(); socket.close(); } grab (ioexception e) { e.printstacktrace(); }
now when run server fine, connect client, when client connects pong ball should sit down still while waits player, ball update without getting info server, 1 time close clients program, server come error
java.net.socketexception: connection reset @ java.net.socketinputstream.read(unknown source) @ sun.nio.cs.streamdecoder.readbytes(unknown source) @ sun.nio.cs.streamdecoder.implread(unknown source) @ sun.nio.cs.streamdecoder.read(unknown source) @ java.io.inputstreamreader.read(unknown source) @ java.io.bufferedreader.fill(unknown source) @ java.io.bufferedreader.readline(unknown source) @ java.io.bufferedreader.readline(unknown source) @ pong.pongplayerthread.run(pongplayerthread.java:42)
the line 42 in pongplayerthread this
if ((line = in.readline()) == null)
i have been trying prepare days, have still not found solution, sense inputstream cannot connect outputstream of client, have tried utilize wireshark lan program, won't work , nil show in wireshark. if shine lite onto this, much appreciated.
canvas
itech update ok have used code here in pongplayerthread now
public void run() { seek { printwriter out = new printwriter(socket.getoutputstream(), true); bufferedreader in = new bufferedreader( new inputstreamreader( socket.getinputstream())); string text = "hhh"; system.out.println(in.tostring()); //system.out.println(text = in.readline()); system.out.println("checking readline value"); string line = null; if ((line = in.readline()) != null) // why check if null !? { system.out.println("client sent: "+line); } while(send) { out.println(pongdata); } out.close(); in.close(); socket.close(); }
this in console "client sent: hello", client not stop , maintain taking in info server,
if set if statement gave me while statement has out.println(pongdata) works error 1 time client connects , disconnects, or error if 2 clients connect , both leave error 1 time again :(
java.net.socketexception: connection reset @ java.net.socketinputstream.read(unknown source) @ sun.nio.cs.streamdecoder.readbytes(unknown source) @ sun.nio.cs.streamdecoder.implread(unknown source) @ sun.nio.cs.streamdecoder.read(unknown source) @ java.io.inputstreamreader.read(unknown source) @ java.io.bufferedreader.fill(unknown source) @ java.io.bufferedreader.readline(unknown source) @ java.io.bufferedreader.readline(unknown source) @ pong.pongplayerthread.run(pongplayerthread.java:45)
and line 45 is
if ((line = in.readline()) != null) // why check if null !?
sorted code out, in pongclient code
while ((pos = serverinput.readline()) != null) { string text = "nothing"; serverout.println(text); string[] posvalues = pos.split(":"); model.getball().setx(double.parsedouble(posvalues[0])); model.getball().sety(double.parsedouble(posvalues[1])); if(pongcontroller.moveup == true) { system.out.println("up"); serverout.println("up"); pongcontroller.moveup = false; } else { serverout.println("nothing"); } }
once hits this, wont anything, , cause whole error again.
i found error, had set
if ((line = in.readline()) != null) {
if set line = in.readline() again, cause error. strange, fixed now, , info can sent client server, , server client :)
once client connects server if ((line = in.readline()) == null)
block thread until message received client.
you need send client after connection established
in pongplayerthread
can modify next , remove else
block
string line = null; if ((line = in.readline()) != null) // why check if null !? { system.out.println("client sent: "+line); }
in pongclient
after establishing socket
connection, can send message, e.g. serverout.println("hello");
java sockets networking tcp
No comments:
Post a Comment