Run function/void in a class extending InputMethodService - java

I have this MainClass which extends the InputMethodService (shortened version):
public class PcAsKeyboard extends InputMethodService implements KeyboardView.OnKeyboardActionListener {
private Keyboard mKeyboard;
private KeyboardView mInputView;
private InputMethodManager mInputMethodManager;
#Override
public void onCreate() {
super.onCreate();
mInputMethodManager = (InputMethodManager)getSystemService(INPUT_METHOD_SERVICE);
ServerThread chatServerThread = new ServerThread(this);
chatServerThread.start();
}
public void output(CharSequence text) {
getCurrentInputConnection().commitText(text, 1); // Sends CharSequence "text" to input field
}
}
Now I want to access the function/void "output" from another class (shortened):
public class ClientThread extends Thread {
// the socket where to listen/talk
Socket socket;
InputStream inputStream;
//InputMethodService ims = new InputMethodService();
String message;
BufferedReader bufferedReader;
//private PcAsKeyboard main = null;
// Constructor
ClientThread(Socket socket, PcAsKeyboard main) {
this.socket = socket;
// Creating stream for data input & a reader for the stream to get the message
try
{
inputStream = socket.getInputStream();
InputStreamReader isReader = new InputStreamReader(inputStream);
bufferedReader = new BufferedReader(isReader);
}
catch (IOException e) {
// Exception creating InputStream!
return;
}
}
// Runs forever
public void run() {
while(true) {
// Trying to get a line from the input stream
try {
message = bufferedReader.readLine();
}
catch (IOException e) {
// "Exception reading InputStream!
break;
}
// TODO: Write var message in input Field
// main.output(message);
// ims.getCurrentInputConnection().commitText(message, 1);
}
try {
inputStream = socket.getInputStream();
} catch (IOException e) {
e.printStackTrace();
}
}
}
At "// TODO: Write var message in input Field" I want to run the output function with the variable "message".
I already tried to transfer the main class (PcAsKeyboard) as a variable when creating the "ClientThread" class but this didn't work/gave null pointer exception. This also happened when I tried to access the "InputMethodService" and calling ".getCurrentInputConnection()" then.
I hope someone of you can help me out with this ^-^

Related

Java Socket ] instance of DataInputStream is print continue null

I'm making Chatting-Room program using the Java Swing.
In the client side, I was saw that doesn't read message from the server side.
The writeUTF() method of the client side is very well and I'm checked readUTF and writeUTF on the server side, that was very well too.
I think the problem is code which does as "Receiver" on the client side.
In the run() method of Thread, The instance dis of the DataInputStream has continuously null value.
I'm so confusing.. Please give me some help.
The bellow is part of my client&server code.
Thanks!
Client code
RoomBackground.java
public class RoomBackground {
private static String socket_server = "127.0.0.1";
private static Socket chatSocket;
private static DataOutputStream dos;
private static DataInputStream dis;
private ChatReceiver chatReceiver;
public Socket getChatSocket() {
return chatSocket;
}
public static DataOutputStream getDos() {
return dos;
}
public RoomBackground() throws IOException {
chatSocket = new Socket(socket_server, 7777);
chatReceiver = new ChatReceiver();
chatReceiver.start();
dos = new DataOutputStream(chatSocket.getOutputStream());
dis = new DataInputStream(chatSocket.getInputStream());
dos.writeUTF(User.getUser().getUsername());
dos.flush();
}
class ChatReceiver extends Thread {
#Override
public void run(){
try {
# PROBLEM CODE..... Allways "dis is null"
System.out.println("dis is " + dis);
# This line never executed.
while(dis != null) {
# some codes.....
}
} catch (IOException e) {
e.printStackTrace();
System.out.println(e.toString());
} catch (BadLocationException e) {
e.printStackTrace();
}
}
}
}
RoomFrame.java
public class RoomFrame extends JFrame{
private RoomBackground roomBackground;
public RoomFrame(int roomId) throws IOException {
chatField.addActionListener(new ActionListener() {
roomBackground = new RoomBackground();
#Override
public void actionPerformed(ActionEvent e) {
String msg = chatField.getText() + "\n";
try {
RoomBackground.getDos().writeUTF(msg);
# It works.
System.out.println("sent msg is " + msg);
} catch (IOException e1) {
e1.printStackTrace();
}
chatField.setText("");
}
});
}
}
Now server code.
Server Background.java
public class ChatReceiver extends Thread {
private DataInputStream in;
private DataOutputStream out;
public ChatReceiver(Socket chatSocket) throws IOException {
out = new DataOutputStream(chatSocket.getOutputStream());
in = new DataInputStream(chatSocket.getInputStream());
nick = in.readUTF();
addChatClient(nick, out);
}
#Override
public void run() {
try {
while(in!=null) {
chatMsg = in.readUTF();
# It works !
System.out.println("before send" + chatMsg);
sendMsg(chatMsg);
# It works too!
System.out.println("after send" + chatMsg);
}
}catch (IOException e) {
removeChatClient(nick);
}
}
}
When you are starting the ChatReceiver thread in the RoomBackground the dis object is not initialized yet, that is why it is null. One solution could be to initialize the dis variable in the ChatReceiver constructor.

java rxtx SerialWriter issue

I am using RXTX to communicate between JAVA and a microcontroller.
This is the JAVA code for opening a connection, sending and receiving data
package app;
import gnu.io.CommPort;
import gnu.io.CommPortIdentifier;
import gnu.io.SerialPort;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
public class SerialCommunication1 {
private static SerialCommunication1 instance = null;
private static boolean coonected = false;
public static SerialCommunication1 getInstance(){
if(instance == null)
instance = new SerialCommunication1();
return instance;
}
private SerialCommunication1() {
super();
try {
connect("COM4");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
SerialCommunication1.coonected = true;
}
void connect(String portName) throws Exception {
CommPortIdentifier portIdentifier = CommPortIdentifier
.getPortIdentifier(portName);
if (portIdentifier.isCurrentlyOwned()) {
System.out.println("Error: Port is currently in use");
} else {
CommPort commPort = portIdentifier.open(this.getClass().getName(),
2000);
if (commPort instanceof SerialPort) {
SerialPort serialPort = (SerialPort) commPort;
serialPort.setSerialPortParams(9600, SerialPort.DATABITS_8,
SerialPort.STOPBITS_2, SerialPort.PARITY_NONE);
InputStream in = serialPort.getInputStream();
OutputStream out = serialPort.getOutputStream();
(new Thread(new SerialReader(in))).start();
(new Thread(new SerialWriter(out))).start();
} else {
System.out
.println("Error: Only serial ports are handled by this example.");
}
}
}
/** */
public static class SerialReader implements Runnable {
InputStream in;
public SerialReader(InputStream in) {
this.in = in;
}
public void run() {
byte[] buffer = new byte[1024];
int len = -1;
try {
while ((len = this.in.read(buffer)) > -1) {
System.out.print(new String(buffer, 0, len));
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
/** */
public static class SerialWriter implements Runnable {
OutputStream out;
static String str = null;
public SerialWriter(OutputStream out) {
this.out = out;
}
public void run() {
System.out.println("Will try to execute");
try {
if(str.length() > 0){
this.out.write(str.getBytes());
str = null;
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
And this is the Java code that is calling when an event triggers
SerialCommunication1.getInstance();
if(ledStatus == true) {SerialCommunication1.SerialWriter.str = "4A01";}
else {SerialCommunication1.SerialWriter.str = "4A00";}
stopProcess();
And now the problem. I need to send a command to my microcontroller with the code 4A01 and, after receiving the answer, I need to call it again with the code 4A00. The calls are triggered by a button from my Java interface. The problem is that the second call is not executed (4A00 is not sending). I tried to inverse the command codes and they work well. After the first one (4A01) is executed, my microcontroller reacts and sends the response which is read by java and my interface is updated. When I send the invers command (4A00) it stops exactly at this line SerialCommunication1.SerialWriter.str = "4A00"; and doesn't even enter inside the SerialWriter's run() method.
Do you have any idea why is this happening? From the side of my microcontroller there is no problem, I checked all the possibilities with a tool.
I hope I made myself clear.
Thank you!
LE: I forgot to tel you that it didn't throw any errors or exceptions
I'm not sure because I'm not able to test your code but I think your problem is in SerialWriter class:
public static class SerialWriter implements Runnable {
OutputStream out;
static String str = null; // Here str is initialized to null
public SerialWriter(OutputStream out) {
this.out = out;
}
public void run() {
System.out.println("Will try to execute");
try {
if(str.length() > 0) { // this should throw NPE because str is null
this.out.write(str.getBytes());
str = null;
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
Since there is no loop in this method, then the thread created within SerialCommunication1 at this line:
(new Thread(new SerialWriter(out))).start();
most likely finishes its execution after sending the first str.
Honestly I don't understand how does it even send a single string, since str is initialized to null in first place and it should throw NullPointerException at str.length() line.
I would suggest you this approach:
Don't trigger a writer thread when connection is established, just trigger a new one every time a message will be sent.
Use Singleton pattern correctly.
Keep a reference to the serial port in SerialCommunication1 class.
Translated to code it would be something like this:
class SerialWriter implements Runnable {
OutputStream out;
String message;
public SerialWriter(OutputStream out) {
this.out = out;
}
public void setMessage(String msg) {
this.message = msg;
}
public void run() {
try {
if(message != null) {
this.out.write(str.getBytes());
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
Then in SerialCommunication1 class add this public method:
public void sendMessage(String msg) {
SerialWriter writer = new SerialWriter(serialPort.getOutputStream()); // of course you'll have to keep reference to serialPort when connection is established
writer.setMessage(msg);
(new Thread(writer)).start();
}
And finally call this method in this way:
SerialCommunication1.getInstance().sendMessage("4A01");
tzortzik,
I think tha is a timeout problem. Try to addding a delay to writer :
/** */
public static class SerialWriter implements Runnable {
OutputStream out;
static String str = null;
public SerialWriter(OutputStream out) {
this.out = out;
}
public void run() {
Thread.sleep(500); //<----------- this should be in mainThread before to SerialWriter.start();
System.out.println("Will try to execute");
try {
if(str.length() > 0){
this.out.write(str.getBytes());
str = null;
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
It happens to me many times, "we should learn to wait for a response" (^_^)
Check if you are executing well a secuence like the next:
Send command 4A01
Receive response 4A01 from micro
WAIT FOR RESPONSE BEFORE SEND SECOND COMMAND. Thread.sleep(500); //wait for 500 milis or more
Send command 4A00
Receive response 4A00 from micro
I hope it could help you.

What character does a BufferedReader interpret as the end of the stream?

When reading from a socket using a BufferedReader it states that the readLine() method returns
A String containing the contents of the line, not including any line-termination characters, or null if the end of the stream has been reached
How does it know that it's reached the end of the stream? What sequence of characters does it use to determine this.
I want to simulate sending the same sequence of characters to properly close another connection that uses PipedStreams.
Edit:
Here is the code in question. From the responses it looks like there is no such sequence and calling close() on the PipedOutput stream should unblock the readLine() on the output stream. It doesn't appear to be doing this at the moment which is why I was confused so I'm thinking it might be a bug somewhere else.
What's happening is the incomingEventIn.close() line appears to be blocking when inputLine = incomingEventIn.readLine() is blocking. If inputLine = incomingEventIn.readLine() isn't being executed on the other thread then incomingEventIn.close() executes fine. Why is this happening?
public class SocketManager {
private Socket socket = null;
private PrintWriter out = null;
private BufferedReader in = null;
private PipedOutputStream incomingEventOutStream = null;
private PrintWriter incomingEventOut = null;
private BufferedReader incomingEventIn = null;
private PipedOutputStream incomingResponsOutStream = null;
private PrintWriter incomingResponseOut = null;
private BufferedReader incomingResponseIn = null;
private ArrayList<AsteriskLiveComsEventListener> listeners = new ArrayList<AsteriskLiveComsEventListener>();
private final ExecutorService eventsDispatcherExecutor;
private String ip;
private int port;
private Object socketLock = new Object();
public SocketManager(String ip, int port) {
this.ip = ip;
this.port = port;
eventsDispatcherExecutor = Executors.newSingleThreadExecutor();
}
public void connect() throws UnableToConnectException, AlreadyConnectedException {
synchronized(socketLock) {
if (socket != null && !socket.isClosed()) {
throw (new AlreadyConnectedException());
}
try {
socket = new Socket(ip, port);
out = new PrintWriter(socket.getOutputStream(), true);
in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
incomingEventOutStream = new PipedOutputStream();
incomingEventIn = new BufferedReader(new InputStreamReader(new PipedInputStream(incomingEventOutStream)));
incomingEventOut = new PrintWriter(incomingEventOutStream);
incomingResponsOutStream = new PipedOutputStream();
incomingResponseIn = new BufferedReader(new InputStreamReader(new PipedInputStream(incomingResponsOutStream)));
incomingResponseOut = new PrintWriter(incomingResponsOutStream);
} catch (IOException e) {
throw (new UnableToConnectException());
}
new Thread(new IncomingEventThread()).start();
new Thread(new SocketThread()).start();
}
}
public void disconnect() throws NotConnectedException {
disconnect(false);
}
private void disconnect(boolean notRequested) throws NotConnectedException {
synchronized(socketLock) {
if (!isConnected()) {
throw (new NotConnectedException());
}
try {
incomingEventIn.close();
} catch (IOException e2) {}
// IT NEVER GETS TO HERE!
incomingEventOut.close();
try {
incomingResponseIn.close();
} catch (IOException e1) {}
System.out.println("disconnecting");
incomingResponseOut.close();
try {
socket.shutdownInput();
} catch (IOException e) {}
try {
socket.shutdownOutput();
} catch (IOException e) {}
try {
socket.close();
} catch (IOException e) {}
if (notRequested) {
System.out.println("disconnecting event");
dispatchEvent(new ConnectionLostEvent());
}
}
}
public boolean isConnected() {
synchronized(socketLock) {
return (socket != null && !socket.isClosed());
}
}
public void addEventListener(AsteriskLiveComsEventListener a) {
synchronized(listeners) {
listeners.add(a);
}
}
public void removeEventListener(AsteriskLiveComsEventListener a) {
synchronized(listeners) {
listeners.remove(a);
}
}
private void dispatchEvent(final AsteriskLiveComsEvent e) {
synchronized (listeners) {
synchronized (eventsDispatcherExecutor) {
eventsDispatcherExecutor.execute(new Runnable()
{
public void run()
{
for(int i=0; i<listeners.size(); i++) {
listeners.get(i).onAsteriskLiveComsEvent(e);
}
}
});
}
}
}
public JSONObject sendRequest(JSONObject request) throws JSONException, NotConnectedException {
synchronized(socketLock) {
System.out.println("sending request "+request.toString());
out.println(request.toString());
try {
return new JSONObject(incomingResponseIn.readLine());
} catch (IOException e) {
// lets close the connection
try {
disconnect(true);
} catch (NotConnectedException e1) {}
throw(new NotConnectedException());
}
}
}
private class SocketThread implements Runnable {
#Override
public void run() {
String inputLine = null;
try {
while((inputLine = in.readLine()) != null) {
// determine if this is a response or event and send to necessary location
JSONObject lineJSON = new JSONObject(inputLine);
if (lineJSON.getString("type").equals("response")) {
incomingResponseOut.println(inputLine);
incomingResponseOut.flush();
}
else if (lineJSON.getString("type").equals("event")) {
incomingEventOut.println(inputLine);
incomingEventOut.flush();
}
}
if (isConnected()) {
try {
disconnect(true);
} catch (NotConnectedException e) {}
}
} catch (IOException e) {
// try and disconnect (if not already disconnected) and end thread
if (isConnected()) {
try {
disconnect(true);
} catch (NotConnectedException e1) {}
}
}
}
}
private class IncomingEventThread implements Runnable {
#Override
public void run() {
String inputLine = null;
try {
while((inputLine = incomingEventIn.readLine()) != null) {
JSONObject lineJSON = new JSONObject(inputLine);
String eventType = lineJSON.getString("eventType");
// determine what type of event it is and then fire one that represents it
if (eventType.equals("channelAdded")) {
JSONObject a = lineJSON.getJSONObject("payload");
Hashtable<String,Object> data = new Hashtable<String,Object>();
Object[] keys = a.keySet().toArray();
for(int i=0; i<keys.length; i++) {
data.put((String) keys[i], a.get((String) keys[i]));
}
dispatchEvent(new ChannelAddedEvent(data));
}
else if (eventType.equals("channelRemoved")) {
dispatchEvent(new ChannelRemovedEvent(lineJSON.getJSONObject("payload").getInt("channelId")));
}
else if (eventType.equals("channelsToRoom")) {
ArrayList<Integer> data = new ArrayList<Integer>();
JSONObject a = lineJSON.getJSONObject("payload");
JSONArray ids = a.getJSONArray("channelIds");
for(int i=0; i<ids.length(); i++) {
data.add(ids.getInt(i));
}
dispatchEvent(new ChannelsToRoomEvent(data));
}
else if (eventType.equals("channelToHolding")) {
dispatchEvent(new ChannelToHoldingEvent(lineJSON.getJSONObject("payload").getInt("channelId")));
}
else if (eventType.equals("channelVerified")) {
dispatchEvent(new ChannelVerifiedEvent(lineJSON.getJSONObject("payload").getInt("channelId")));
}
else if (eventType.equals("serverResetting")) {
dispatchEvent(new ServerResettingEvent());
}
}
} catch (IOException e) {}
System.out.println("here");
}
}
Edit 2:
I think it's a deadlock issue somewhere because if I put some breakpoints in before it in the debugger it runs fine and inputLine = incomingEventIn.readLine() returns null. If I try and run it normally it locks up.
Edit 3: Solved thanks to Gray's answer. The input stream is being closed before the output which was causing the lock up. It needs to be the other way around. Closing the output stream first then informs the input stream that the stream is closed and unblocks the readLine() method.
How does it know that it's reached the end of the stream? What sequence of characters does it use to determine this.
The answer to this is OS dependent but the OS' I'm familiar with, no EOF characters are read. The OS returns to the underlying caller the return values that indicate that the stream (file-descriptor) has reached EOF. The JVM sees the return value and returns the appropriate return (null, -1, ...) to the InputStream or Reader caller depending on the method.
I want to simulate sending the same sequence of characters to properly close another connection that uses PipedStreams.
If you are reading from a PipedReader then you close the associated PipedWriter. The Reader or InputStream will then return the appropriate EOF value to the caller.
Edit:
Since your IncomingEventThread is reading from incomingEventIn, the disconnect() method should close the incomingEventOut first. The thread should close the in side itself. Then you should close the response out.
I would not have the thread call disconnect(...). It should only close it's reader and writer, not all of the streams.
Check out this question:
what is character for end of file of filestream?
There isn't one. The OS knows when the stream reaches its end via the file size, the TCP FIN bit, or other out-of-band mechanisms depending on the source. The only exception I'm aware of is that the terminal driver recognizes Ctrl/d or Ctrl/z as EOF when types by a keyboard, but again that's the OS, not the Java stream or reader.
From your point of view, just call close on PipedOutputStream that you use to connect to your test.
The actual close of the socket is performed by the TCP stack on client and server.
This should do (note that you cannot read/write piped streams on the same thread, hence the 2 methods and a thread creation):
void runTest ( final PipedInputStream sink ) throws Exception
{
try( final PipedOutputStream stream = new PipedOutputStream( sink ) )
{
try ( final OutputStreamWriter swriter =
new OutputStreamWriter( stream, "UTF-8" )
)
{
try ( final PrintWriter writer = new PrintWriter( swriter ) )
{
writer.println( "Hello" );
writer.println( "World!" );
}
}
}
}
void test ( final PipedInputStream sink ) throws InterruptedException
{
final Thread outputThread =
new Thread(
new Runnable ( )
{
#Override
public void run ( )
{
try
{
runTest( sink );
}
catch ( final Exception ex )
{
throw new RuntimeException( ex );
}
}
}
);
outputThread.start( );
outputThread.join( );
}

Using ordinary stream writers/readers with piped streams

Consider the following test code.
I am trying to find out if I can use piped streams like "normal" I/O streams, together with the commonly used Reader and Writer implementations (specifically, another part of the code base I am working on demands that I use OutputStreamWriter).
The problem here is that nothing appears to show up on the read end. The program at least appears to correctly write the message to the write-end of the pipe, but when trying to read from the other end I block indefinetly, or if I (as in this case) check for available bytes, the call returns 0.
What am I doing wrong?
public class PipeTest {
private InputStream input;
private OutputStream output;
public PipeTest() throws IOException {
input = new PipedInputStream();
output = new PipedOutputStream((PipedInputStream)input);
}
public void start() {
Stuff1 stuff1 = new Stuff1(input);
Stuff2 stuff2 = new Stuff2(output);
Thread thread = new Thread(stuff1);
thread.start();
Thread thread2 = new Thread(stuff2);
thread2.start();
}
public static void main(String[] args) throws IOException {
new PipeTest().start();
}
private static class Stuff1 implements Runnable {
InputStream inputStream;
public Stuff1(InputStream inputStream) {
this.inputStream = inputStream;
}
#Override
public void run() {
String message;
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
try {
//message = reader.readLine();
System.out.println("Got message!");
System.out.println(inputStream.available());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
private static class Stuff2 implements Runnable {
OutputStream outputStream;
public Stuff2(OutputStream outputStream) {
this.outputStream = outputStream;
}
#Override
public void run() {
String message = "Hej!!\n";
OutputStreamWriter writer = new OutputStreamWriter(outputStream);
try {
writer.write(message);
System.out.println("Wrote message!");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
As you are never reading from the read end, it is impossible to see how you could possibly arrive at that conclusion, and any such conclusion is therefore baseless and invalid.
All you are doing is printing available() at an arbitrary point in time, which isn't sufficient to prove that nothing ever shows up at the read end.

java socket programming problem (sending and receiving data)

Client has sendpoints() method which is called by some other class that I did not include.
Anyways, sendpoints() is called and sends integers to the server, which receives them and send back to all the clients that are connected to the server(broadcast).
The problem is, clients keep sending integers while server is stuck in the thread I created for receiving integers(I think the server is not reading from inputstream).
I tried changing the stream, I tried putting integers together in a object and send it with ObjectOutputStream but none of these seems to work.
I need help (pointStruct is a class that holds some integer values I created)
import java.net.*;
import java.util.*;
import java.awt.*;
import java.io.*;
import javax.swing.*;
public class Server {
private ArrayList dataclient;
private ArrayList messageclient;
private ServerSocket dataserver;
private ServerSocket messageserver;
public static void main(String[] args) {
Server s1 = new Server();
s1.start();
}
// Start running server
public void start() {
try {
dataserver = new ServerSocket(4999);
messageserver = new ServerSocket(5000);
Socket dataconn;
Socket messageconn;
dataclient = new ArrayList();
messageclient = new ArrayList();
dataconn= null;
messageconn= null;
System.out.println("[server]start");
//start accepting connections
while (true) {
try {
dataconn = dataserver.accept();
System.out.println("[server]accepted dataconn");
messageconn = messageserver.accept();
System.out.println("[server]accepted messageconn");
//add clients to arraylist
dataclient.add(dataconn.getOutputStream());
messageclient.add(messageconn.getOutputStream());
}
catch (Exception ex) {
ex.printStackTrace();
}
//creating receiver threads
Thread t1 = new Thread(new DataReceiver(dataconn));
Thread t2 = new Thread(new MessageReceiver(messageconn));
System.out.println("[server]Thread successfully created");
t1.start();
t2.start();
System.out.println("[server]Thread successfully started");
}
}
catch(Exception ex) {
ex.printStackTrace();
}
}
//receive data from clients
public class DataReceiver implements Runnable {
BufferedReader br;
InputStream is;
int x,y;
int x2,y2;
int t;
int red;
int green;
int blue;
int size;
int dummy;
DataReceiver(Socket s){
try {
is=s.getInputStream();
//br = new BufferedReader(isr);
}
catch(Exception ex) {
ex.printStackTrace();
}
}
public void run() {
while(true) {
try{
Iterator it = dataclient.iterator();
dummy=is.read();
if(dummy==9999) {
System.out.println("[server]executing data thread");
x=is.read();
System.out.println("[server]read a line"+x);
y=is.read();
System.out.println("[server]read a line"+y);
//x2=isr.read();
//y2=isr.read();
t=is.read();
red=is.read();
green=is.read();
blue=is.read();
size=is.read();
dummy=0;
//broadcast data
while (it.hasNext()) {
OutputStream os = (OutputStream)it.next();
os.write(9999);
os.write(x);
os.write(y);
os.write(t);
os.write(255);
os.write(0);
os.write(0);
os.write(size);
}
System.out.println("[server]data broadcasted");
}
}
catch(Exception ex) {
ex.printStackTrace();
}
}
}
}
//------------------------receive message from clients------------------------
public class MessageReceiver implements Runnable {
MessageReceiver(Socket s) {
}
public void run() {
}
}
}
public class networkHandler{
PrintWriter writer;
BufferedReader reader;
PrintWriter pwriter;
BufferedReader preader;
Socket sock;
Socket pointsock;
InputStream is;
JTextArea incoming;
pointHandler ph;
public networkHandler(pointHandler _ph) {
init();
ph=_ph;
setUpNetworking();
Thread readerThread = new Thread(new IncomingReader());
readerThread.start();
Thread pointerThread = new Thread(new ReceivingPoints());
pointerThread.start();
}
public void init() {
incoming = new JTextArea(20,20);
}
private void setUpNetworking() {
try {
// setup message port
System.out.println("networking establish started");
sock = new Socket("127.0.0.1",5000);
System.out.println("[NH]port 5000 established");
// setup point port
pointsock = new Socket("127.0.0.1",4999);
System.out.println("[NH]port 4999 established");
//message i/o stream
InputStreamReader streamReader = new InputStreamReader(sock.getInputStream());
reader = new BufferedReader(streamReader);
writer = new PrintWriter(sock.getOutputStream());
//point i/o stream
InputStreamReader pstreamReader = new InputStreamReader(pointsock.getInputStream());
System.out.println("networking establishing: Stream");
preader= new BufferedReader(pstreamReader);
pwriter= new PrintWriter(pointsock.getOutputStream());
System.out.println("networking establishing: Stream");
}
catch(IOException ex) {
ex.printStackTrace();
}
System.out.println("networking established");
}
//send message to the server
public void writeStream(String input){
try {
writer.println(input);
writer.flush();
}
catch(Exception ex) {
ex.printStackTrace();
}
}
public JTextArea getServerMessage() {
return incoming;
}
//receiving message from server
public class IncomingReader implements Runnable {
#Override
public void run() {
String message;
try {
while ((message = reader.readLine())!=null){
System.out.println("[NH] read from server:"+message);
incoming.append(message+"\n");
}
}
catch(Exception ex) {
ex.printStackTrace();
}
}
}
//receiving points from server
public class ReceivingPoints implements Runnable {
int x,y;
int x2,y2;
int red;
int green;
int blue;
int t;
int size;
int dummy;
pointStruct ps;
Color cr;
Point p;
synchronized public void run() {
try {
is = pointsock.getInputStream();
p= new Point();
}
catch(Exception ex) {
ex.printStackTrace();
}
while(true) {
try {
dummy=is.read();
if(dummy==9999) {
x=is.read();
y=is.read();
//x2=preader.read();
//y2=preader.read();
t=is.read();
red=is.read();
green=is.read();
blue =is.read();
size=is.read();
//create dummy pointStruct
ps = new pointStruct();
cr = new Color(red,green,blue);
p.x=x;
p.y=y;
ps.setP1(p);
p.x=x2;
p.y=y2;
//ps.setP2(p);
ps.setT((char)t);
ps.setS(size);
ps.setC(cr);
ph.save(ps);
dummy=0;
}
}
catch(Exception ex) {
ex.printStackTrace();
}
System.out.println("[NH]receiving done");
}
}}
public void sendPoints(pointStruct ps) {
OutputStream os;
try{
os=pointsock.getOutputStream();
os.write(9999);
os.write(ps.getP1().x);
os.write(ps.getP1().y);
//pwriter.print(ps.getP2().x);
//pwriter.print(ps.getP2().y);
os.write(ps.getT());
os.write(ps.getC().getRed());
os.write(ps.getC().getGreen());
os.write(ps.getC().getBlue());
os.write(ps.getS());
}
catch(Exception ex) {
ex.printStackTrace();
}
System.out.println("[NH]points sent to server");
}
}
You are reading the stream incorrectly, InputStream.read() returns a byte from the stream, but cast to an int.
InputStream.read() returns values from 0 to 255 is read is successful, and -1 if no more reading can be done (end of stream).
For example, InputStream.read() != 9999 always. So this ReceivingPoints.run() block will not fire:
while (true) {
try {
dummy = is.read();
if (dummy == 9999) {}
} catch(Exception ex) {
ex.printStackTrace();
}
}
You are looking for DataInputStream, it has methods for reading and writing other basic types than just bytes.

Categories

Resources