Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.DataInputStream;
- import java.io.DataOutputStream;
- import java.io.IOException;
- import java.net.Socket;
- public final class ClientSocket implements Runnable {
- private final Socket socket;
- private String user;
- private String pass;
- private static DataInputStream dis;
- private static DataOutputStream dos;
- private static PacketHandler packetHandler = new PacketHandler();
- public ClientSocket(Socket socket, String user, String pass) {
- this.socket = socket;
- if (socket.isClosed()) {
- return;
- }
- this.user = user;
- this.pass = pass;
- try {
- dis = new DataInputStream(socket.getInputStream());
- dos = new DataOutputStream(socket.getOutputStream());
- } catch (IOException e) {
- return;
- }
- }
- /**
- * Initializes the login procedure
- */
- private void sendLoginPacket() {
- if (writeOpcode(45)) {
- writeString(user);
- writeString(pass);
- //int resp = readByte();
- //System.out.println("Resp: " + resp);
- } else {
- }
- }
- @Override
- public void run() {
- if (socket == null) {
- System.out.println("NO1");
- return;
- }
- if (socket.isClosed()) {
- System.out.println("NO2");
- return;
- }
- sendLoginPacket();
- try {
- for (;;) {
- /*if (socket == null)
- return;
- if (socket.isClosed())
- return;*/
- int opcode = -1;
- //System.out.println("Waiting for opcode");
- if ((opcode = dis.readByte()) != -1) {
- System.out.println("Opcode: " + opcode);
- switch (opcode) {
- case 99:
- //dos.writeByte(254);
- //dos.writeLong(System.currentTimeMillis());
- break;
- case 25:
- break;
- /*case 0:
- dos.writeByte(0);
- break;
- case 111:
- int returnValue = dis.readInt();
- System.out.println(returnValue + " return value");
- break;*/
- default:
- System.out.println("Packet [" + opcode + "] unhandled");
- packetHandler.handlePacket(this, opcode);
- break;
- }
- }
- }
- } catch (IOException e) {
- try {
- socket.close();
- } catch (IOException e1) {
- e1.printStackTrace();
- }
- return;
- } catch (NullPointerException npe) {
- return;
- }
- }
- public void writeKeepAlive() {
- try {
- if (!(dos == null))
- dos.writeByte(0);
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- public void writeBytes(byte[] bytes) {
- try {
- if (!(dos == null)) {
- for (int i = 0; i < bytes.length; i++) {
- dos.writeByte(bytes[i]);
- }
- }
- //dos.write(bytes);
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- public boolean write(byte[] write, int offset, int len) {
- try {
- if (!(dos == null))
- dos.write(write, offset, len);
- } catch (IOException e) {
- return false;
- }
- return true;
- }
- public void sendBytes(byte[] myByteArray) throws IOException {
- sendBytes(myByteArray, 0, myByteArray.length);
- }
- public void sendBytes(byte[] myByteArray, int start, int len) throws IOException {
- if (len < 0)
- throw new IllegalArgumentException("Negative length not allowed");
- if (start < 0 || start >= myByteArray.length)
- throw new IndexOutOfBoundsException("Out of bounds: " + start);
- // Other checks if needed.
- // May be better to save the streams in the support class;
- // just like the socket variable.
- dos.writeInt(len);
- if (len > 0) {
- dos.write(myByteArray, start, len);
- }
- }
- public boolean writeInt(int write) {
- try {
- if (!(dos == null))
- dos.writeInt(write);
- } catch (IOException e) {
- return false;
- }
- return true;
- }
- public boolean writeOpcode(int opcode) {
- try {
- if (!(dos == null))
- dos.writeByte(opcode);
- } catch (IOException e) {
- return false;
- }
- return true;
- }
- public boolean writeShort(short s) {
- try {
- if (!(dos == null))
- dos.writeShort(s);
- } catch (IOException e) {
- return false;
- }
- return true;
- }
- public boolean writeBoolean(boolean bool) {
- try {
- if (!(dos == null))
- dos.writeBoolean(bool);
- } catch (IOException e) {
- return false;
- }
- return true;
- }
- public byte readByte() {
- try {
- return dis.readByte();
- } catch (IOException e) {
- //e.printStackTrace();
- }
- return -1;
- }
- public byte[] readBytes(int amount) {
- byte[] data = new byte[amount];
- for (int i = 0; i < amount; i++) {
- data[i] = (byte) readByte();
- }
- return data;
- }
- public int readInt() {
- try {
- return dis.readInt();
- } catch (IOException e) {
- //e.printStackTrace();
- }
- return -1;
- }
- public short readShort() {
- try {
- return dis.readShort();
- } catch (IOException e) {
- //e.printStackTrace();
- }
- return -1;
- }
- public long readLong() {
- try {
- return dis.readLong();
- } catch (IOException e) {
- //e.printStackTrace();
- }
- return -1;
- }
- public boolean readBoolean() {
- try {
- return dis.readBoolean();
- } catch (IOException e) {
- //e.printStackTrace();
- }
- return false;
- }
- public boolean writeString(String s) {
- try {
- if (!(dos == null)) {
- dos.writeInt(s.length());
- for (int i = 0; i < s.length(); i++) {
- dos.writeChar(s.charAt(i));
- }
- }
- // dos.writeUTF(s);
- } catch (IOException e) {
- return false;
- }
- return true;
- }
- public String readString() {
- StringBuilder sb = new StringBuilder();
- try {
- //return dis.readUTF();
- if (!(dis == null)) {
- int len = dis.read();
- for (int i = 0; i < len; i++) {
- sb.append(dis.readChar());
- }
- }
- } catch (IOException e) {
- //e.printStackTrace();
- }
- return sb.toString();
- }
- public void close() {
- try {
- dis.close();
- if (!(dos == null))
- dos.flush();
- if (!(dos == null))
- dos.close();
- socket.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- public boolean writeByte(byte b) {
- try {
- if (!(dos == null)) {
- dos.writeByte(b);
- }
- } catch (IOException e) {
- return false;
- }
- return true;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement