Sending PrintWriter to textArea using OutputStream - java

I have a program which is to be a chat program between a client and server. I set the sockets up depending on which button was clicked like this:
public void actionPerformed(ActionEvent e)
{
Object o = e.getSource();
if(o==startServer)
{
startServer.enable(false);
try{
listenSocket = new ServerSocket(port);
Socket client = listenSocket.accept();
InputStreamReader is = new InputStreamReader(client.getInputStream());
BufferedReader buffRdr = new BufferedReader(is);
PrintWriter printWrt = new PrintWriter(client.getOutputStream(),true);
service = 1;
sendButton.enable(true);
disconnectButton.enable(true);
sendText.enable(true);
more = true;
}catch(IOException ie){
ie.printStackTrace();
}
}
if(o==connectButton)
{
try{
startServer.enable(false);
connectButton.enable(false);
Socket server = new Socket(host,port);
InputStreamReader is = new InputStreamReader(server.getInputStream());
BufferedReader buffRdr = new BufferedReader(is);
PrintWriter printWrt = new PrintWriter(server.getOutputStream(),true);
service = 2;
sendButton.enable(true);
disconnectButton.enable(true);
sendText.enable(true);
more = true;
}catch(IOException ie){
ie.printStackTrace();
}
}
I run two instances of the program and everything seems to connect but im not sure how the program knows to get the text from the sendText textBox i have set up. Here is how i have my run set up but it never gets to the System.out.println("Service Changed") or where i append "Listening to socket" after the program waits for the service to change from 0.
while(more)
{
statusArea.append("Chat Running");
String line;
try{
System.out.println(service);
while(service==0 && more);
System.out.println("Service Changed");
if(buffRdr!=null)
{
statusArea.append("Listening to socket");
while(more && (line=buffRdr.readLine())!=null)
{
chatArea.append(line);
}
}
}catch(IOException e)
{
}
}
Any help on this would be appreciated, im rather new at programming in java

Related

How to send data to server outside the client class

I am writing a server-client program. It is like a simple console chat room. I use server thread to handle multiple clients. It works well if I put the Scanner inside the client constructor.(This part is commented) But, I want to move the Scanner part which requests input from user to the main. I got socket close error.I don't know what the problem is. How can I use send function to send data in main?
Client:
public class GameClient {
private Socket socket;
private String serverIP;
private OutputStreamWriter writer;
private BufferedReader reader;
public GameClient(String host){
this.serverIP = host;
try{
//Connect to server
socket = new Socket(InetAddress.getByName(serverIP), 1234);
writer = new OutputStreamWriter(this.socket.getOutputStream(), "UTF-8");
reader = new BufferedReader(new InputStreamReader(this.socket.getInputStream(), "UTF-8"));
//Start a new thread for reading from server
new Thread(new GameClientReader(socket)).start();
Scanner scanner = new Scanner(System.in);
System.out.println("Write something: ");
String str = "";
// while((str = scanner.nextLine()) != null){
// writer.write(str);
// writer.write('\n');
// writer.flush();
// System.out.println("Write something: ");
// }
}catch(IOException e){
System.out.println("Client failed to connect!");
e.printStackTrace();
}finally{
try {
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public void send(JSONObject json){
try{
String message = json.toJSONString();
writer.write(message);
writer.write('\n');
writer.flush();
}catch(IOException e){
e.printStackTrace();
}
}
public void send(String msg){
try {
writer.write(msg);
writer.flush();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String args[]){
GameClient client = new GameClient("127.0.0.1");
Scanner scanner = new Scanner(System.in);
System.out.println("Write something: ");
String str = "";
while((str = scanner.nextLine()) != null){
client.send(str);
System.out.println("Write something: ");
}
}
}
You close Socket in finally block of GameClient constructor, and then call method send which calls writer.write(msg); that uses instance of Socket

Multithreading with sockets JAVA

I have a problem with multithreading. I don't know why, but threads run in sequence.
There is a client-server application. I need to run several parallel threads for message exchange. The whole classes are too big, so I will show main parts.
Code of Client(This code runs every time when I push the button):
public ArrayList<Action> call() {
Thread myThready = new Thread(new Runnable()
{
public void run()
{
BufferedReader in;
PrintWriter out;
try{
Socket fromserver = new Socket(ip, PortID);
in = new BufferedReader(new InputStreamReader(fromserver.getInputStream()));
out = new PrintWriter(fromserver.getOutputStream(),true);
writeLog(query, ID, 0);
out.println(query+"ID"+ID);
String fserver = in.readLine();
writeLog(fserver, ID, 1);
out.println("exit");
out.close();
in.close();
fromserver.close();
}
catch (IOException io){
return;
}
}
});
myThready.start();
}
Code of Server:
public void run(){
flag=true;
System.out.println("Welcome to Server side!");
createLog();
ExecutorService service = Executors.newCachedThreadPool();
ServerSocket servers = null;
int n=4600;
try{
servers = new ServerSocket(n);
} catch( Exception e){
}
Socket fromclient = null;
while(true){
try {
System.out.print("Waiting for a client...");
fromclient = servers.accept();
System.out.println("Client connected.");
Callable<ArrayList<Action>> callable = new HandleThread(fromclient);
Future<ArrayList<Action>> future = service.submit(callable);
list.addAll(future.get());
} catch (Exception e) {
System.out.println("Can't accept.");
System.exit(-1);
}
}
}
This code does "accept()" and then creates new thread for some calculations.
Code of HandleThread:
public ArrayList<Action> call() {
BufferedReader in = null;
PrintWriter out= null;
try {
in = new BufferedReader(new
InputStreamReader(fromclient.getInputStream()));
out = new PrintWriter(fromclient.getOutputStream(),true);
String input,output;
System.out.println("Wait for messages.");
while ((input = in.readLine()) != null) {
//close filewriter thread if input==exit
if(input.equalsIgnoreCase("exit")){
break;
}
System.out.println(input);
String[] arr = input.split("ID");
System.out.println("+"+arr[0]);
ID = Integer.parseInt(arr[1]);
writeLog(arr[0], ID, 0);
process(arr[0], ID);
out.println(arr[0]);
writeLog(arr[0], ID, 1);
}
out.close();
in.close();
fromclient.close();
} catch(IOException e){
return null;
}
return list;
}
I don't know why this doesn't work. I have logs and I see that one thread runs only after another one. Not at the same time!
Please, help me!
Future#get() is a blocking call.
list.addAll(future.get());
The calling thread will wait until the task is done. As such, your server thread which calls accept() waits for each task to finish before it gets to the next one.

Socket data does not appear to be getting through to client

I've written some serverside socket handling code and I'm concerned that potentially my packets are not always making it back to the client. I am logging all my events and in my log files it says I am sending the information. But the client is also logging events and in their logs they say they do not receive anything.
My code to send the data is as follows:
public void write(Packet packet) {
String data = packet.serialize();
log("Send=[" + data + "]", "Write"); // log to file
try {
_writer.write(data);
_writer.flush();
} catch (Exception ex) {
log(ex, "write");
}
}
Each socket is created on a new thread and I create my writers and readers immediately like so (in the public run method):
// _sockt is a Java Socket object
_writer = new BufferedWriter(new OutputStreamWriter(_socket
.getOutputStream()));
_reader = new SocketReader(_socket);
SocketReader is just a wrapper class I created for listening for responses and has a public read method like so:
public String read() throws IOException, SocketTimeoutException {
_socket.setSoTimeout(_timeOut);
if(_reader == null)
_reader = new BufferedReader(new InputStreamReader(_socket.getInputStream()));
// read from the stream
return new PacketDataInputStream(_reader).read();
}
The PacketDataInputStream wrapper class:
BufferedReader _reader = null;
public PacketDataInputStream(BufferedReader reader)
{
_reader = reader;
}
public String read() throws IOException, SocketException {
StringBuilder builder = new StringBuilder();
int c = 0;
while((c = _reader.read()) != -1)
{
char ch = (char)c;
builder.append(ch);
if(ch == PacketConstants.ETX)
break;
}
if(builder.length() > 0)
return builder.toString();
else
return null;
}
The way I'm creating the actual socket listener objects is pretty standard I think:
InetAddress address = InetAddress.getByName(IP);
server = new ServerSocket( port, 0, address);
// My own manager class to handle all the sockets connected
WebSocketManager manager = new WebSocketManager(this);
Socket connection = null;
while(bContinue)
{
connection = server.accept();
if(bContinue) {
// assign the socket to a new thread and start
// that thread
manager.newSocket(connection);
} else {
connection.close();
}
}
Is is possible that I'm using the wrong objects for sending the data back.
Should I even be using a bufferedwriter and reader? I had thought that these were the best way to go but now I'm not so sure.
It's important to note that this does not happen all the time, just sporadically. It could be the clients code having bugs but I need to make sure that I'm doing it correctly before going back to them.
This code is run on a Linux Ubuntu server. Logging occurs to a text file, nothing special there. My log files show the Send="" data going back to the client and no exception so it appears as if the .write and .flush() worked? Socket connections are persistant and only closed by the client and or network issues.
UPDATE ----- Client Side code -------:
I did manage to get some of the client side code for how they are handling the send and receiving of data (just in case it's more obvious on their end). The client is actually connecting to this server via an Android device (if that helps).
Creation of socket
static final int BUFFER_SIZE = 20000; // Maximum packet size
java.net.InetAddress server = java.net.InetAddress.getByName(url);
socket = new Socket(server, port);
// Set socket options:
socket.setReceiveBufferSize(BUFFER_SIZE);
socket.setSendBufferSize(BUFFER_SIZE);
socket.setKeepAlive(true);
socket.setTcpNoDelay(true);
Sending:
try {
// Send the packet:
OutputStream stream = socket.getOutputStream();
stream.write(p.getByteArray ());
stream.flush();
// Update the time:
lastPacketSendTime = new Date ();
} catch (IOException e) {
setError("Error sending packet (" + e.getMessage() + ")", ERROR_IO);
return false;
}
Receiving:
socket.setSoTimeout(timeout);
// Get the reader:
inputStream = socket.getInputStream();
while (true) {
// Get the next character:
int value = inputStream.read();
// Check for -1, indicating that the socket is closed:
if (value == -1) {
// The socket is closed remotely, so close it locally as well:
disconnect();
inputStream = null;
return null;
}
// ... and a bunch of other stuff to handle the actual data
}
EDIT 14-Nov:
This is actually proving to be more of a problem now. Both the client logs and the server logs appear to be sending. But at times the data doesn't appear to come through or if it does it is sometimes coming through 10 - 30 - 60 second delayed.
I can provide more information if required.
When you use BufferedReaders and BufferedWriters things get buffered. How about using the input and output streams directly.. Also, writers are character based, I don't know if you need to send binary data but if so that will be a problem with writers.
I am not sure whether this will be to your any use or not.. but i am giving you the code i used for client server communication..
Client Side:
public class ClientWala {
public static void main(String[] args) throws Exception{
Boolean b = true;
Socket s = new Socket("127.0.0.1", 4444);
System.out.println("connected: "+s.isConnected());
OutputStream output = s.getOutputStream();
PrintWriter pw = new PrintWriter(output,true);
// to write data to server
while(b){
if (!b){
System.exit(0);
}
else {
pw.write(new Scanner(System.in).nextLine());
}
}
// to read data from server
InputStream input = s.getInputStream();
InputStreamReader isr = new InputStreamReader(input);
BufferedReader br = new BufferedReader(isr);
String data = null;
while ((data = br.readLine())!=null){
// Print it using sysout, or do whatever you want with the incoming data from server
}
}
}
Server Code:
import java.io.*
import java.net.*;
public class ServerTest {
ServerSocket s;
public void go() {
try {
s = new ServerSocket(44457);
while (true) {
Socket incoming = s.accept();
Thread t = new Thread(new MyCon(incoming));
t.start();
}
} catch (IOException e) {
e.printStackTrace();
}
}
class MyCon implements Runnable {
Socket incoming;
public MyCon(Socket incoming) {
this.incoming = incoming;
}
#Override
public void run() {
try {
PrintWriter pw = new PrintWriter(incoming.getOutputStream(),
true);
InputStreamReader isr = new InputStreamReader(
incoming.getInputStream());
BufferedReader br = new BufferedReader(isr);
String inp = null;
boolean isDone = true;
System.out.println("TYPE : BYE");
System.out.println();
while (isDone && ((inp = br.readLine()) != null)) {
System.out.println(inp);
if (inp.trim().equals("BYE")) {
System.out
.println("THANKS FOR CONNECTING...Bye for now");
isDone = false;
s.close();
}
}
} catch (IOException e) {
// TODO Auto-generated catch block
try {
s.close();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
e.printStackTrace();
}
}
}
public static void main(String[] args) {
new ServerTest().go();
}
}

Difficulty with socket communication from Server to Client

I have a relatively simple program where I try establish Client Server connection and at the same time I use threads in the client side to allow for multiple connections.
I run the server and then the server invokes the client constructor and passes the port connection to the client and the thread is started on the client side.
The problem I have is that when I run the server side it doesn't want to go beyond the constructor call. It seems to get stuck at the constructor.
Sorry all this sounds a bit confusing
Any thoughts perhaps
this is the server side
ServerMultipleThreads()
{
System.out.println("Starting the server first...");
try
{
ServerSoc = new ServerSocket(7777);
listening = true;
}
catch(Exception e)
{
System.out.println(e.toString());
System.exit(1);
}
System.out.println("The server has started running");
while(listening)
{
try
{
//creating the client socket and starting the new client session
new ClientSession(ServerSoc.accept());
System.out.println("The clientSession was called");
in = new DataInputStream(clientSocket.getInputStream());
BufferedReader is = new BufferedReader(new InputStreamReader(in));
os = new PrintStream(clientSocket.getOutputStream());
while(true)
{
line = is.readLine();
PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("myFile,txt")), true);
out.println(line);
}
}
catch(IOException ioe)
{
System.out.println(ioe.toString());
}
}
}
and this is on client side
ClientSession(Socket s)
{
clientSocket = s;
try
{
out = new PrintWriter(clientSocket.getOutputStream(), true);
in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
out.println("Welcome");
}
catch(IOException exe)
{
System.out.println(exe.toString());
}
//starting the thread
while(runner == null)
{
runner = new Thread(this);
runner.start();
}
}
public void run()
{
while(runner == Thread.currentThread())
{
BufferedReader buf = new BufferedReader(new InputStreamReader(System.in));
String stdIn;
try
{
while((stdIn = buf.readLine()) != null)
{
out.println(stdIn);
}
}
catch(IOException exe)
{
exe.toString();
}
try
{
Thread.sleep(10);
}
catch(InterruptedException e){}
}
Kind regards
Arian
That is because ServerSocket.accept() blocks until it receives a client request.
You need to have a client calling the server, something like this:
Socket socket = new Socket(host, port);
InputStream in = socket.getInputStream();
// write some data...

Server and ServerSocket in one Application: not working

I am trying to write a small program, that opens a server, creates a client that connects to this server and receives a message from it.
This is the Code so far
public static void main(String[] args) {
final ServerSocket serverSocket;
try {
serverSocket = new ServerSocket(12345);
Thread t = new Thread(){
public void run(){
try {
Socket server = serverSocket.accept();
PrintWriter writer = new PrintWriter(server.getOutputStream(), true);
writer.write("Hello World");
writer.flush();
} catch (IOException e) {
e.printStackTrace();
}
}
};
t.start();
Socket client = new Socket("localhost", 12345);
BufferedReader reader = new BufferedReader(new InputStreamReader(client.getInputStream()));
String message = reader.readLine();
System.out.println("Received " + message);
} catch (IOException e1) {
e1.printStackTrace();
}
}
If i run program it keeps waiting in readLine() - so obviously the client does not receive the message from the server.
Has anyone got an idea why this isn' working?
Your reading thread is waiting for a newline in the data stream. Just change the server to use:
writer.write("Hello World\r\n");
and you'll get the result you were expecting. Alternatively, you can just close the server socket, and then readLine will return when it reaches the end of the data stream.
You should put the readline in a loop as follows:
public static void main(String[] args) {
final ServerSocket serverSocket;
try {
serverSocket = new ServerSocket(12345);
Thread t = new Thread() {
public void run() {
try {
Socket server = serverSocket.accept();
PrintWriter writer = new PrintWriter(server.getOutputStream(), true);
writer.write("Hello World");
writer.flush();
} catch (IOException e) {
e.printStackTrace();
}
}
};
t.start();
Socket client = new Socket("localhost", 12345);
BufferedReader reader = new BufferedReader(new InputStreamReader(client.getInputStream()));
// Check this --------------------------------------------------->
String message = null;
while ((message = in.readLine()) != null) {
System.out.println("Received " + message);
break; //This break will exit the loop when the first message is sent by the server
}
} catch (IOException e1) {
e1.printStackTrace();
}
}
You can read this documentation for further explanation: http://download.oracle.com/javase/tutorial/networking/sockets/

Categories

Resources