Yeap I tested it with println, it wasn't calling those methods. Eventually I added run() method to the player constructor.
Weird how the tutorial excluded that.
The guy who wrote the tutorial also failed to explain a bit and he was using deprecated information.
Going to look for a new one.
So I've got a new question, if I want to send/receive data through a buffer over the network which classes/methods would I need, I'm not sure which they're called for example "DataOutputStream".
So far what I've got down is creating the server socket which is just one line of code, creating a boolean and then a do until loop for the main method which keeps the server alive, I then have a hashmap or arraylist which ever works to reference new Player instances, I look for incoming connections and when one is found I store the socket and name in a new object Player then increase the player count. I dont know how to loop for disconnections yet because that requires the player 'pinging' the server every 30 seconds or so. If the server receives it the client is still connected else destroy that player object and decrease player count.
What I'd like to do next is receive a message from the client, I've created a chatbox on the client and prepared a send message button. My format is msg_type, msg. Msg_type represents an integer from 1-XXX and depending on the message type the server can anticipate a different msg using a switch case statement. This is what I tried..
package mypackage;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
import java.net.SocketTimeoutException;
import java.io.BufferedReader;
public class Player {
private DataOutputStream output;//define a data output that we will later use to send messages to the client
private DataInputStream input; //define a data input stream that we will later us to read messages from the client;
private Socket socket;// define a socket
private boolean alive = true; //define a boolean to keep the player class running
private String name; //define a string that will hold the player name
public Player(String nm, Socket sck) //constructor or method ?
{
socket =sck; //set the received socket to the player class
name = nm;// set the player name
try {
socket.setSoTimeout(5000);//Set the socket timeout to 30 seconds
output = new DataOutputStream(socket.getOutputStream()); //get the data output stream and set it to output
output.flush();//flush away all the bytes on the data output stream
input = new DataInputStream(socket.getInputStream()); //get the data input stream and set it to input
run();
} catch(IOException e){}
}
public void run()
{
do
{ //start a do loop while alive is true
checkForMessages();//check for messages
System.out.println("Run method..");
}
while (alive);
}
private void checkForMessages()
{
int received_type;
String receive_text;
System.out.println("CheckForMessages method..");
try{
received_type = input.readInt();
if (received_type != 0)
{
System.out.println(received_type);
}
if (received_type == 1)
{
//1 means textmsg
receive_text = input.readLine();
System.out.println(receive_text);
}
} catch(IOException e){}
}
}
So I'm using data input and dataoutput streams, then remembering the format I'm using msg_type and msg which I call received_type and receive_text, first I check received_type, if it's not == 0 then print the type. If it's equal to 1 then read the next received buffer and print it.
My problem is receive_text = input.readLine();
.readline() is deprecated.
It's recommending I use BufferedStream or something like that.
I believe I'm going at reading all wrong here.