Java Sockets Game error -
i followed tutorial(https://www.youtube.com/watch?v=xkbyce59y9w) , has left me error:
unable start client java.net.socketexception: connection reset @ java.net.socketinputstream.read(unknown source) @ java.net.socketinputstream.read(unknown source) @ java.net.socketinputstream.read(unknown source) @ java.io.datainputstream.readint(unknown source) @ client.client.init(client.java:39) @ sun.applet.appletpanel.run(unknown source) @ java.lang.thread.run(unknown source)
code: client
package client; import java.applet.applet; import java.awt.graphics; import java.awt.event.keyevent; import java.awt.event.keylistener; import java.io.datainputstream; import java.io.dataoutputstream; import java.io.ioexception; import java.net.socket; public class client extends applet implements runnable, keylistener { private static final long serialversionuid = 1l; static socket socket; static datainputstream in; static dataoutputstream out; int playerid; int[] x = new int[10]; int[] y = new int[10]; boolean left, down, right, up; int playerx; int playery; @override public void init() { setsize(100, 100); addkeylistener(this); try { system.out.println("conectando..."); socket = new socket("localhost", 7777); system.out.println("conectado"); in = new datainputstream(socket.getinputstream()); playerid = in.readint(); out = new dataoutputstream(socket.getoutputstream()); input input = new input(in, this); thread thread = new thread(input); thread.start(); thread thread2 = new thread(this); thread2.start(); } catch (exception e) { system.out.println("unable start client"); e.printstacktrace(); } } public void updatecoordinates(int pid, int x, int y) { this.x[pid] = x; this.y[pid] = y; } @override public void paint(graphics g) { (int = 0; < 10; i++) { g.drawoval(x[i], y[i], 5, 5); } } @override public void run() { while (true) { if (right == true) { playerx += 10; } if (left == true) { playerx -= 10; } if (down == true) { playery += 10; } if (up == true) { playery -= 10; } if (right || left || || down) { try { out.writeint(playerid); out.writeint(playerx); out.writeint(playery); } catch (ioexception e) { system.out.println("error sending coordinates"); } } repaint(); try { thread.sleep(400); } catch (interruptedexception e) { e.printstacktrace(); } } } @override public void keypressed(keyevent e) { if (e.getkeycode() == 37) { left = true; } if (e.getkeycode() == 38) { = true; } if (e.getkeycode() == 39) { right = true; } if (e.getkeycode() == 40) { down = true; } } @override public void keyreleased(keyevent e) { if (e.getkeycode() == 37) { left = false; } if (e.getkeycode() == 38) { = false; } if (e.getkeycode() == 39) { right = false; } if (e.getkeycode() == 40) { down = false; } } @override public void keytyped(keyevent e) { } } class input implements runnable { datainputstream in; client client; public input(datainputstream in, client c) { this.in = in; this.client = c; } @override public void run() { while (true) { try { int playerid = in.readint(); int x = in.readint(); int y = in.readint(); client.updatecoordinates(playerid, x, y); } catch (ioexception e) { e.printstacktrace(); } } } }
server
package server; import java.io.datainputstream; import java.io.dataoutputstream; import java.io.ioexception; import java.net.serversocket; import java.net.socket; public class server { static serversocket serversocket; static socket socket; static dataoutputstream out; static users[] user = new users[10]; static datainputstream in; public static void main(string[] args) throws exception { system.out.println("opening server..."); serversocket = new serversocket(7777); system.out.println("server opened..."); while (true) { socket = serversocket.accept(); (int = 0; < 10; i++) { if (user[i] == null) { system.out.println("ip: " + socket.getinetaddress() + " pid: " + user[i].playerid); out = new dataoutputstream(socket.getoutputstream()); in = new datainputstream(socket.getinputstream()); user[i] = new users(out, in, user, i); thread thread = new thread(user[i]); thread.start(); break; } } } } } class users implements runnable { dataoutputstream out; datainputstream in; users[] user = new users[10]; string name; int playerid; int playeridin; int xin; int yin; public users(dataoutputstream out, datainputstream in, users[] user, int pid) { this.out = out; this.in = in; this.user = user; this.playerid = pid; } @override public void run() { try { out.writeint(playerid); } catch (ioexception e1) { system.out.println("playerid error"); } while (true) { try { playeridin = in.readint(); xin = in.readint(); yin = in.readint(); (int = 0; < 10; i++) { if (user[i] != null) { user[i].out.writeint(playeridin); user[i].out.writeint(xin); user[i].out.writeint(yin); } } } catch (ioexception e) { user[playerid] = null; } } } }
i think mistake may because version used in tutorial older help
in server side change.
if (user[i] != null) { system.out.println("ip: " + socket.getinetaddress() + " pid: " + user[i].playerid); out = new dataoutputstream(socket.getoutputstream()); in = new datainputstream(socket.getinputstream()); user[i] = new users(out, in, user, i); thread thread = new thread(user[i]); thread.start(); break; }
instead of
if (user[i] == null) { system.out.println("ip: " + socket.getinetaddress() + " pid: " + user[i].playerid); out = new dataoutputstream(socket.getoutputstream()); in = new datainputstream(socket.getinputstream()); user[i] = new users(out, in, user, i); thread thread = new thread(user[i]); thread.start(); break; }
Comments
Post a Comment