Socket not sending to Client or Client not recieving - java

I have a Server class and a Client class which is run on an android device. Basically, I need to load the user's friends and their IP address. The user's friends are stored in an ArrayList. So the client must send the friend's name and the server will respond with an IP address if the friend is online or a "-1" if the friend is offline. The server has access to all the users and stores it in an Arraylist of my defined type. My problem is that the client doesn't receive the IP Address/"-1" from the server, so only one friend is checked as the server is waiting for a response. The Login of Client is done elsewhere and is working perfectly.
Server:
public class Server {
private static final int port = 9001;
private static final String IPAddr = "xxx.xxx.xx.10";
ServerSocket server = null;
ArrayList <Client> users = new ArrayList<Client>();
public Server(){
readUsers();
try{
server = new ServerSocket(port);
System.out.println("connected server on port" + port);
while(true){
System.out.println("waiting for connection my ip add is "+ InetAddress.getLocalHost().getHostAddress());
Socket clientsocket = server.accept();
System.out.println("Connect to client:"+ clientsocket.getInetAddress().getHostName());
ClientThread client = new ClientThread(clientsocket);
client.start();
}
} catch(IOException e) {
System.err.println("Could not listen on port");
}
}
public void readUsers() {
//read form user text file on start up
ReadFile reader = new ReadFile("users.txt");
try{
String[] data = reader.OpenFile();
for (int i = 0 ; i<data.length; i++){
String[] parts = data[i].split(" ");
ArrayList<String> ips = new ArrayList<String>();
for(int j =2; j<parts.length; j++){
ips.add(parts[j]);
}
Client clnt = new Client(parts[0],parts[1], ips);
users.add(clnt);
}
}catch(Exception e){
System.err.println("Couldn't read in data");
}
}
public void RegisterUser(){
//append to user text if a new user registers
}
//Thread
public class ClientThread extends Thread {
private Socket sckt = null;
private String purpose;
ObjectOutputStream objectOutput;
ObjectInputStream objectInput;
public ClientThread(Socket sckt){
super("ClientThread");
this.sckt = sckt;
}
public void run(){
try{
objectOutput = new ObjectOutputStream(sckt.getOutputStream());
objectOutput.flush();
objectInput = new ObjectInputStream(sckt.getInputStream());
purpose = objectInput.readUTF();
if(purpose.contains("login")){
LoginUser();
}else {
LoadClientIP();
}
} catch(Exception e){
System.err.println("Couldnt read purpose: "+ purpose);
}
}
public void LoginUser(){
try{
String Username = objectInput.readUTF();
String Password = objectInput.readUTF();
int ClientIndex = isClient(Username);
if (ClientIndex != -1){
if(users.get(ClientIndex).password.equals(Password)){
//password correct -> send friends
users.get(ClientIndex).online = true;
users.get(ClientIndex).SetCurrentIP(sckt.getRemoteSocketAddress().toString());
objectOutput.writeUTF("correct");
System.out.println(Username + " is correct");
LoadClientFriends(Username, ClientIndex);
objectOutput.writeObject(users.get(ClientIndex).Friends);
System.out.println("Friends sent");
} else {
//password incorrect -> retry
objectOutput.writeUnshared("password");
System.out.println(Username + " has wrong password");
}
} else {
//not a registered client
objectOutput.writeUTF("wrong");
System.out.println(Username + " is not a client");
}
} catch(Exception e){
System.err.println("Couldnt connect to Client socket");
}
}
public void LoadClientIP(){
try{
int size = Integer.parseInt(objectInput.readUTF()); //keep track of the number of friends
System.out.println("The size of friends:"+ size);
for (int j =0; j<size; j++){
String client = objectInput.readUTF();
System.out.println("Client: "+ client);
int i = isClient(client); //index of friend is user arraylist
if (users.get(i).online == true){
String IP_add = users.get(i).IP.get(users.get(i).CurrentIP);
objectOutput.writeUTF(IP_add);
System.out.println("Client is at index i "+ i + " with IP "+ IP_add);
}else {
objectOutput.writeUTF("-1");
System.out.println("Client is not online");
}
}
System.out.println("out of while");
}catch (Exception e){
System.err.println("Couldn't load IP");
}
}
}
public void LoadClientFriends(String name, int index){
//upon a client signing in, server must load his friends contacts
ReadFile reader = new ReadFile(name+".txt");
try{
String[] data = reader.OpenFile();
for (int i = 0 ; i<data.length; i++){
users.get(index).Friends.add(data[i]);
}
}catch(Exception e){
System.err.println("Couldn't read in friend data");
}
}
public int isClient(String name){
boolean isclient = false;
int i =0;
int index =-1;
while((isclient == false) && (i<users.size())){
if (name.equals(users.get(i).Username)){
isclient = true;
index = i;
}else {
i++;
}
}
return index;
}
public static void main(String[] args){
Server svr = new Server();
}
}
Client/Android:
public class chat_screen extends ActionBarActivity {
ArrayList<String> friends;
ArrayList<String> onlineIP;
ArrayList<String> onlineFriend;
private static final int port = 9001;
private static final String IPAddr = "xx.x.x.4";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.chat_menu);
friends = (ArrayList<String>) getIntent().getSerializableExtra("Friends");
Thread FriendThread = new Thread(Connect);
FriendThread.start();
}
//Thread
Runnable Connect = new Runnable()
{
public void run()
{
try {
//Problems: getting stuck when reading in friends, only is able to send the first friend
Socket connection = new Socket(IPAddr,port);
ObjectOutputStream objectout = new ObjectOutputStream(connection.getOutputStream());
objectout.flush();
ObjectInputStream objectin = new ObjectInputStream(connection.getInputStream());
//send purpose
objectout.writeUTF("load");
objectout.flush();
objectout.writeUTF(Integer.toString(friends.size()));
objectout.flush();
//send friends name to get IP
for(int i =0; i<friends.size(); i++){
objectout.writeUTF(friends.get(i));
objectout.flush();
String ip = objectin.readUTF(); //stuck on this line
if ((ip.contains("-1"))== false){
onlineIP.add(ip);
onlineFriend.add(friends.get(i));
}else {
//do nothing
}
}
connection.close();
}catch (Exception e){
finish();
}
}
};
}

Related

Wait for client to enter name before broadcasting the server message

From GameServer.java:
while (true) {
Socket clientSocket = serverSocket.accept();
PlayerThread playerThread = new PlayerThread(clientSocket, this);
playerThreads.add(playerThread);
playerThread.start();
if (playerThreads.size() == maxPlayers) {
broadcast("All players have joined! Let the game begin!", null);
}
}
From PlayerThread.java:
#Override
public void run() {
try {
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
out = new PrintWriter(socket.getOutputStream(), true);
out.println("What's your name? ");
String userName = in.readLine();
Player player = new Player(userName);
gameServer.addPlayer(p);
String clientMove;
while ((clientMove = in.readLine()) != null) {
gameServer.makeMove(player, clientMove);
gameServer.broadcast(p.getName() + " made the move: " + clientMove, this);
}
} catch (IOException e) {
e.printStackTrace();
}
}
Starting the server:
Client1 at the end:
Client2 at the end:
How do I wait for the last client (here, client2) to enter their name before broadcasting the message?
Here's the broadcast function:
public void broadcast(String message, PlayerThread excluded) {
for (PlayerThread p : playerThreads) {
if (p != excluded)
p.sendMessage(message);
}
}
Use CountDownLatch:
private CountDownLatch countDownLatch = new CountDownLatch(maxPlayers);
So, after playerThreads.size() == maxPlayers reached, call await:
if (playerThreads.size() == maxPlayers) {
countDownLatch.await();
broadcast("All players have joined! Let the game begin!", null);
}
You need to pass this CountDownLatch to every PlayThread, and after username is read, call CountDownLatch#countDown:
public PlayerThread(Socket socket, GameServer gameServer, CountDownLatch countDownLatch) throws IOException {
downLatch = countDownLatch;
}
#Override
public void run() {
try {
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
out = new PrintWriter(socket.getOutputStream(), true);
out.println("What's your name? ");
String userName = in.readLine();
downLatch.countDown();
} catch (IOException e) {
e.printStackTrace();
}
}

Java SocketChannel not receiving incoming data properly

I'm writing a chat room server which takes message from a chat client and broadcasts out the message to all users. This is an exercise from a book called An Introduction to Network Programming with Java: Java 7 Compatible with which I'm self-teaching Java networking basics. I wrote a GUI frontend for the chat room and implemented the server backend, following examples from the code in the book. However, when I tested the code with chat clients, the server seemed unable to receive clients' data. I can't figure out why. The code for the chat room (here it was made in command line mode for test purpose) and the client is as followings. Thank you.
// code for the server backend, problem seems to be lie in here.
/**
* The multiecho server itself
*/
package channelEchoServer;
import java.net.*;
import java.io.*;
import java.nio.*;
import java.nio.channels.*;
import java.util.*;
public class MultiEchoServerNIO {
private static ServerSocketChannel serverSocketChannel;
private static final int PORT = 1234;
private static Selector selector;
private static Vector<SocketChannel> socketChannelVec;
private static Vector<ChatUser> allUsers;
public static final int CAPACITY = 20;
public static final int BUFFER_SIZE = 2048;
public static final String NEW_LINE = System.lineSeparator();
public static void main(String[] args) {
ServerSocket serverSocket = null;
try {
serverSocketChannel = ServerSocketChannel.open();
serverSocketChannel.configureBlocking(false);
serverSocket = serverSocketChannel.socket();
InetSocketAddress netAddress = new InetSocketAddress(PORT);
serverSocket.bind(netAddress);
selector = Selector.open();
serverSocketChannel.register(selector, SelectionKey.OP_ACCEPT);
}
catch (IOException ioEx) {
ioEx.printStackTrace();
System.exit(1);
}
socketChannelVec = new Vector<>(CAPACITY);
allUsers = new Vector<>(CAPACITY);
System.out.println("Server is opened ...");
processConnections();
}
private static void processConnections () {
do {
try {
int numKeys = selector.select();
System.out.println(numKeys + " keys selected.");
if (numKeys > 0) {
Set eventKeys = selector.selectedKeys();
Iterator keyCycler = eventKeys.iterator();
while (keyCycler.hasNext()) {
SelectionKey key = (SelectionKey)keyCycler.next();
int keyOps = key.readyOps();
if ((keyOps & SelectionKey.OP_ACCEPT) == SelectionKey.OP_ACCEPT) {
acceptConnection(key);
continue;
}
if ((keyOps & SelectionKey.OP_READ) == SelectionKey.OP_READ) {
acceptData(key);
}
}
}
}
catch (IOException ioEx) {
ioEx.printStackTrace();
System.exit(1);
}
} while (true);
}
private static void acceptConnection (SelectionKey key) throws IOException {
SocketChannel socketChannel;
Socket socket;
socketChannel = serverSocketChannel.accept();
socketChannel.configureBlocking(false);
socket = socketChannel.socket();
System.out.println("Connection on " + socket + ".");
socketChannel.register(selector, SelectionKey.OP_READ);
socketChannelVec.add(socketChannel);
selector.selectedKeys().remove(key);
}
private static void acceptData (SelectionKey key) throws IOException {
SocketChannel socketChannel;
Socket socket;
ByteBuffer buffer = ByteBuffer.allocate(BUFFER_SIZE);
socketChannel = (SocketChannel) key.channel();
buffer.clear();
int numBytes = socketChannel.read(buffer);
socket = socketChannel.socket();
if (numBytes == -1) {
key.cancel();
closeSocket(socket);
}
else {
String chatName = null;
byte[] byteArray = buffer.array();
if (byteArray[0] == '#')
announceNewUser(socketChannel, buffer);
else {
for (ChatUser chatUser : allUsers)
if (chatUser.getUserSocketChannel().equals(socketChannel))
chatName = chatUser.getChatName();
broadcastMessage(chatName, buffer);
}
}
}
private static void closeSocket (Socket socket) {
try {
if (socket != null)
socket.close();
}
catch (IOException ioEx) {
System.out.println("Unable to close socket!");
}
}
public static void announceNewUser (SocketChannel userSocketChannel, ByteBuffer buffer) {
ChatUser chatUser;
byte[] byteArray = buffer.array();
int messageSize = buffer.position();
String chatName = new String(byteArray, 1, messageSize);
if (chatName.indexOf("\n") >= 0)
chatName = chatName.substring(0, chatName.indexOf("\n"));
chatUser = new ChatUser(userSocketChannel, chatName);
allUsers.add(chatUser);
if (!socketChannelVec.remove(userSocketChannel)) {
System.out.println("Can't find user!");
return;
} // we should save userSocketChannel in a chatUser instance before deleting it.
chatName = chatUser.getChatName();
System.out.println(chatName + " entered the chat room at " + new Date() + "." + NEW_LINE);
String welcomeMessage = "Welcome " + chatName + "!" + NEW_LINE;
byte[] bytes = welcomeMessage.getBytes();
buffer.clear();
for (int i = 0; i < welcomeMessage.length(); i++)
buffer.put(bytes[i]);
buffer.flip();
try {
chatUser.getUserSocketChannel().write(buffer);
}
catch (IOException ioEx) {
ioEx.printStackTrace();
}
}
public static void announceExit (String name) {
System.out.println(name + " left chat room at " + new Date() + "." + NEW_LINE);
for (ChatUser chatUser : allUsers) {
if (chatUser.getChatName().equals(name))
allUsers.remove(chatUser);
}
}
public static void broadcastMessage (String chatName, ByteBuffer buffer) {
String messagePrefix = chatName + ": ";
byte[] messagePrefixBytes = messagePrefix.getBytes();
final byte[] CR = NEW_LINE.getBytes();
try {
int messageSize = buffer.position();
byte[] messageBytes = buffer.array();
byte[] messageBytesCopy = new byte[messageSize];
String userMessage = new String(messageBytes, 0, messageSize);
if (userMessage.equals("Bye"))
announceExit(chatName);
for (int i = 0; i < messageSize; i++)
messageBytesCopy[i] = messageBytes[i];
buffer.clear();
buffer.put(messagePrefixBytes);
for (int i = 0; i < messageSize; i++)
buffer.put(messageBytesCopy[i]);
buffer.put(CR);
SocketChannel chatSocketChannel;
for (ChatUser chatUser : allUsers) {
chatSocketChannel = chatUser.getUserSocketChannel();
buffer.flip();
chatSocketChannel.write(buffer);
}
}
catch (IOException ioEx) {
ioEx.printStackTrace();
}
}
}
class ChatUser {
private SocketChannel userSocketChannel;
private String chatName;
public ChatUser (SocketChannel userSocketChannel, String chatName) {
this.userSocketChannel = userSocketChannel;
this.chatName = chatName;
}
public SocketChannel getUserSocketChannel () {
return userSocketChannel;
}
public String getChatName () {
return chatName;
}
}
// Code for a chat client for testing purpose
package multithreadEchoChatroomClientGUI;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
import java.net.*;
import java.util.*;
public class MultithreadEchoChatroomClient1 {
private static Socket socket;
private static InetAddress host;
private static String address;
public static final int PORT = 1234;
public static void main(String[] args) {
address = JOptionPane.showInputDialog("Enter the host name or IP address:");
try {
host = InetAddress.getByName(address);
}
catch (UnknownHostException uhEx) {
JOptionPane.showMessageDialog(null, "Unknown Host!", "Error", JOptionPane.ERROR_MESSAGE);
System.exit(1);
}
try {
socket = new Socket(host, PORT);
}
catch (IOException ioEx) {
JOptionPane.showMessageDialog(null, ioEx.toString(), "Error", JOptionPane.ERROR_MESSAGE);
System.exit(1);
}
SwingUtilities.invokeLater(new Runnable () {
public void run () {
ClientFrame client = new ClientFrame(host, socket, address);
client.setTitle("Chat");
client.setSize(400, 500);
client.setVisible(true);
new Thread(client).start();
}
});
}
}
class ClientFrame extends JFrame implements Runnable {
private InetAddress host;
private String address;
private Socket socket;
private Scanner input;
private PrintWriter output;
private JMenuItem connect;
private JTextArea serverResponseArea;
private JTextArea messageArea;
private JTextField messageFiled;
private JButton sendButton;
private String serverResponse;
private String clientName;
public ClientFrame (InetAddress host, Socket socket, String address) {
this.host = host;
this.socket = socket;
this.address = address;
initFrame();
}
public void run () {
try {
input = new Scanner(socket.getInputStream());
output = new PrintWriter(socket.getOutputStream(), true);
}
catch (IOException ioEx) {
JOptionPane.showMessageDialog(this, "Cannot create input or output stream!", "Error", JOptionPane.ERROR_MESSAGE);
closeSocket();
System.exit(1);
}
do {
clientName = JOptionPane.showInputDialog("What nickname would you like to use in the chatroom?");
} while (clientName == null);
output.println("#" + clientName);
do {
serverResponse = input.nextLine();
serverResponseArea.append(serverResponse + "\n");
} while (socket.isClosed() != true);
}
private final void closeSocket () {
try {
socket.close();
}
catch (IOException ioEx) {
JOptionPane.showMessageDialog(this, "Cannot disconnect from chatroom!", "Error", JOptionPane.ERROR_MESSAGE);
}
}
private void initFrame () {
JMenuBar menuBar = createMenuBar();
setJMenuBar(menuBar);
JScrollPane responsePanel = createResponsePanel();
add(responsePanel, BorderLayout.NORTH);
JPanel messagePanel = createMessagePanel();
add(messagePanel, BorderLayout.CENTER);
//JPanel textPanel = createTextPanel();
//add(textPanel, BorderLayout.SOUTH);
addWindowListener(new WindowAdapter () {
#Override
public void windowClosing (WindowEvent we) {
if (!socket.isClosed())
closeSocket();
System.exit(0);
}
});
}
private JMenuBar createMenuBar () {
JMenuBar menuBar = new JMenuBar();
JMenu menu = new JMenu("Operations");
connect = new JMenuItem("Connect");
connect.setEnabled(false);
connect.addActionListener(new ActionListener () {
#Override
public void actionPerformed (ActionEvent event) {
try {
host = InetAddress.getByName(address);
}
catch (UnknownHostException uhEx) {
JOptionPane.showMessageDialog(null, "Unknown Host!", "Error", JOptionPane.ERROR_MESSAGE);
System.exit(1);
}
try {
socket = new Socket(host, MultithreadEchoChatroomClient1.PORT);
}
catch (IOException ioEx) {
JOptionPane.showMessageDialog(null, ioEx.toString(), "Error", JOptionPane.ERROR_MESSAGE);
}
try {
input = new Scanner(socket.getInputStream());
output = new PrintWriter(socket.getOutputStream(), true);
}
catch (IOException ioEx) {
JOptionPane.showMessageDialog(ClientFrame.this, "Cannot create input or output stream!", "Error", JOptionPane.ERROR_MESSAGE);
closeSocket();
System.exit(1);
}
output.println("#" + clientName);
serverResponse = input.nextLine();
serverResponseArea.append(serverResponse + "\n");
}
});
JMenuItem quit = new JMenuItem("Quit");
quit.addActionListener(new ActionListener() {
#Override
public void actionPerformed (ActionEvent event) {
if (!socket.isClosed())
closeSocket();
System.exit(0);
}
});
menu.add(connect);
menu.add(quit);
menuBar.add(menu);
return menuBar;
}
private JScrollPane createResponsePanel () {
serverResponseArea = new JTextArea(20, 35);
serverResponseArea.setEditable(false);
serverResponseArea.setLineWrap(true);
serverResponseArea.setWrapStyleWord(true);
serverResponseArea.setMargin(new Insets(5, 5, 5, 5));
JScrollPane scrlPane = new JScrollPane(serverResponseArea);
scrlPane.setBorder(BorderFactory.createEmptyBorder(20, 10, 10, 20));
scrlPane.setBackground(Color.yellow);
return scrlPane;
}
private JPanel createMessagePanel () {
JPanel msgPanel = new JPanel();
msgPanel.setBorder(BorderFactory.createEmptyBorder(20,10, 10, 20));
msgPanel.setBackground(Color.blue);
msgPanel.setLayout(new BoxLayout(msgPanel, BoxLayout.LINE_AXIS));
JScrollPane srlPanel = createMessageTextPanel();
msgPanel.add(srlPanel);
JButton sdButton = createSendButton();
msgPanel.add(sdButton);
return msgPanel;
}
private JScrollPane createMessageTextPanel () {
messageArea = new JTextArea(10, 35);
//messageArea.setEditable(false);
messageArea.setLineWrap(true);
messageArea.setWrapStyleWord(true);
messageArea.setMargin(new Insets(5, 5, 5, 5));
JScrollPane mtPanel = new JScrollPane(messageArea);
return mtPanel;
}
private JButton createSendButton () {
JButton button = new JButton("Send");
button.addActionListener(new ActionListener() {
#Override
public void actionPerformed (ActionEvent event) {
String message;
message = messageArea.getText();
System.out.println(message);
output.println(message);
if (message.equals("Bye")) {
closeSocket();
connect.setEnabled(true);
}
messageArea.setText("");
//serverResponse = input.nextLine();
//serverResponseArea.append(serverResponse + "\n");
}
});
return button;
}
}
Thank you again for taking so much trouble reading much code and giving suggestions!
The problem is solved according to #user207421. I should have put a remove() operation in the else {} block in the method acceptData(SelectedKey key) so that the old key associated with the socketChannel is removed from the selected key set for new incoming keys to be able to be detected.
I don't know whether my understanding of user207421's solution was right or not, but it did solve the problem for now. If anybody has any other ideas, please share your views.
Thank you again, user207421. And thank all visitors to this post for your attention.

Client/Server Chat app with file transfer JAVA Sockets

I am new to java and I made a chat application through which client and server can send and receive messages, now I was trying to send a file from client to server but after the file is received by server , client and server both cant send messages , Here is the code :
Client Side :
import java.io.*;
import java.net.*;
import javax.swing.JFileChooser;
public class ClientFrame extends javax.swing.JFrame {
static Socket s;
static DataOutputStream dos1;
static DataInputStream dis;
static javax.swing.JTextArea jT;
static JFileChooser fc = new JFileChooser();
File file;
public ClientFrame() {
initComponents();
jT = jTextArea1;
}
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
//send message
try {
String str1 = jTextField1.getText();
String o = jT.getText() + "\n" + " Client ->" + str1;
jT.setText(o);
dos1.writeUTF(str1);
} catch (Exception ex) {
}
}
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
//open file chooser
fc.showOpenDialog(this);
file = fc.getSelectedFile();
jTextField3.setText(file.getAbsolutePath());
}
private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {
//send file
try {
String st = jT.getText() + "\n" + " Client -> " + "Sending a file";
jT.setText(st);
String str1 = "Client Sending a file,Press 'REC File' ";
String st1 = "\n" + " Client ->" + str1;
dos1.writeUTF(st1);
} catch (Exception e) {
}
long length = file.length();
byte[] bytes = new byte[65536];//65536 is max, i think
InputStream in;
try {
in = new FileInputStream(file);
OutputStream out = s.getOutputStream();
int count;
while ((count = in.read(bytes)) > 0) {
out.write(bytes, 0, count);
}
out.close();
in.close();
s_r_m();
} catch (Exception ex) {
}
}
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new ClientFrame().setVisible(true);
}
});
try {
s = new Socket("localhost", 3000);
} catch (Exception e) {
}
s_r_m();
}
public static void s_r_m() {
System.out.println("call srm");
try {
dis = new DataInputStream(s.getInputStream());
dos1 = new DataOutputStream(s.getOutputStream());
while (true) {
String str = (String) dis.readUTF();
String out = jT.getText() + "\n" + " Server ->" + str;
jT.setText(out);
}
} catch (Exception ex) {
}
}
}
Server Side :
import java.io.*;
import java.net.*;
import javax.swing.JFileChooser;
public class ServerFrame extends javax.swing.JFrame {
static ServerSocket ss;
static Socket s;
static DataInputStream dis;
static DataOutputStream dos1;
static javax.swing.JTextArea jT;
static JFileChooser fc = new JFileChooser();
File file;
public ServerFrame() {
initComponents();
jT = jTextArea1;
}
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
//send message
try {
String str1 = jTextField1.getText();
String out = jT.getText() + "\n" + " Server ->" + str1;
jT.setText(out);
dos1.writeUTF(str1);
} catch (Exception ex) {
}
}
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
//open file chooser
fc.showOpenDialog(this);
file = fc.getSelectedFile();
jTextField3.setText(file.getAbsolutePath());
}
private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {
//rec file
InputStream in = null;
OutputStream out = null;
try {
in = s.getInputStream();
out = new FileOutputStream("F:\\yoMama.exe");
int count;
byte[] buffer = new byte[65536];
while ((count = in.read(buffer)) > 0) {
out.write(buffer, 0, count);
}
String o = jT.getText() + "\n" + " Client ->" + " File received";
jT.setText(o);
out.close();
in.close();
s_r_m();
} catch (Exception ex) {
}
}
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new ServerFrame().setVisible(true);
}
});
try {
ss = new ServerSocket(3000);
s = ss.accept();//will accept connection from client,waiting state untill client connects
s_r_m();
} catch (Exception e) {
}
}
public static void s_r_m() {
System.out.println("call srm");
try {
dis = new DataInputStream(s.getInputStream());
dos1 = new DataOutputStream(s.getOutputStream());
while (true) {
String str = dis.readUTF();
String out = jT.getText() + "\n" + " Client ->" + str;
jT.setText(out);
}
} catch (Exception ex) {
}
}
}
The problem is in the s_r_m() function. In while loop first statement is String str = dis.readUTF(); So here both Client and Server will wait for the reply from the other side first which will end up in a Deadlock. So Any of them won't be able to send any data till the receive from the other side.
So there you need to change code accordingly. I have implemented a code to solve this problem which takes input from Key-Board(STDIN).
DataInputStream dis=new DataInputStream(sckt.getInputStream());
DataInputStream dis1=new DataInputStream(System.in);
DataOutputStream dos=new DataOutputStream(sckt.getOutputStream());
String str="";
while(true)
{
while(dis.available()>0) //If input is available from the other side.
{
String out = jT.getText() + "\n" + " Client ->" + dis.readUTF();
jT.setText(out);
}
while(dis1.available()>0) //If input is available from STDIN to send it to the other side.
{
str=sc.nextLine();
dos.writeUTF(str);
dos.flush();
}
}
Here you can change code of taking input from STDIN to Text-Field you have in your application. So Whenever a user press Send Button , It will send the message to the other side.

create new thread for every client

I have a piece of code that connects a client to my server. However, every time I connect a client to the server it controls the same player instead of creating a new player. I think it is because I am not creating a new thread for every new client. If someone could show me how to create a new thread for every client that joins using the code I have already written.
import java.net.*;
import java.io.*;
public class Client extends PlayGame
{
public static void main(String [] args)
{
//String serverName = args[0];
//int port = Integer.parseInt(args[1]);
String serverName = "localhost";
int port = 40004;
//while (true ) {
try
{
System.out.println("Connecting to " + serverName + " on port " + port);
Socket client = new Socket(serverName, port);
System.out.println("Just connected to " + client.getRemoteSocketAddress());
OutputStream outToServer = client.getOutputStream();
DataOutputStream out = new DataOutputStream(outToServer);
//out.writeUTF("Hello from " + client.getLocalSocketAddress());
//DataInputStream in = new DataInputStream(inFromServer);
//System.out.println("Server says " + in.readUTF());
PlayGame game = new PlayGame();
//System.out.println("Do you want to load a specitic map?");
//System.out.println("Press enter for default map");
//game.selectMap(game.readUserInput());
//System.out.println("You may now use MOVE, LOOK, QUIT and any other legal commands");
String input = game.readUserInput();
while (input != "quit") {
out.writeUTF( input );
InputStream inFromServer = client.getInputStream();
DataInputStream in = new DataInputStream(inFromServer);
System.out.println("Server Response:\n" + in.readUTF());
input = game.readUserInput();
}
//game.update();
//client.close();
}catch(IOException e)
{
e.printStackTrace();
}
//}
}
}
Server class and ClientThread subclass
import java.net.*;
import java.util.Random;
import java.io.*;
public class Server implements IGameLogic
{
private ServerSocket serverSocket;
private Map map = null;
private int[] playerPosition;
private int collectedGold;
private boolean active;
public Server(int port) throws IOException
{
serverSocket = new ServerSocket(port);
//serverSocket.setSoTimeout(10000);
}
public void startServer()
{
map = new Map();
setMap(new File("example_map.txt"));
System.out.println("Game Started - Map Initialized");
while(true)
{
try
{
System.out.println("Waiting for client on port " + serverSocket.getLocalPort() + "...");
Socket server = serverSocket.accept();
ClientThread ct = new ClientThread(server, this );
ct.start();
}catch(SocketTimeoutException s)
{
System.out.println("Socket timed out!");
break;
}catch(IOException e)
{
e.printStackTrace();
break;
}
}
}
public void setMap(File file) {
map.readMap(file);
playerPosition = initiatePlayer();
active = true;
}
/**
* Prints how much gold is still required to win!
*/
public synchronized String hello() {
return "GOLD: " + (map.getWin() - collectedGold);
}
/**
* By proving a character direction from the set of {N,S,E,W} the gamelogic
* checks if this location can be visited by the player.
* If it is true, the player is moved to the new location.
* #return If the move was executed Success is returned. If the move could not execute Fail is returned.
*/
public synchronized String move(char direction) {
int[] newPosition = playerPosition.clone();
switch (direction){
case 'N':
newPosition[0] -=1;
break;
case 'E':
newPosition[1] +=1;
break;
case 'S':
newPosition[0] +=1;
break;
case 'W':
newPosition[1] -=1;
break;
default:
break;
}
if(map.lookAtTile(newPosition[0], newPosition[1]) != '#'){
playerPosition = newPosition;
if (checkWin())
quitGame();
return "SUCCESS";
} else {
return "FAIL";
}
}
public synchronized String pickup() {
if (map.lookAtTile(playerPosition[0], playerPosition[1]) == 'G') {
collectedGold++;
map.replaceTile(playerPosition[0], playerPosition[1], '.');
return "SUCCESS, GOLD COINS: " + collectedGold;
}
return "FAIL" + "\n" + "There is nothing to pick up...";
}
/**
* The method shows the dungeon around the player location
*/
public synchronized String look() {
String output = "";
char [][] lookReply = map.lookWindow(playerPosition[0], playerPosition[1], 5);
lookReply[2][2] = 'P';
for (int i=0;i<lookReply.length;i++){
for (int j=0;j<lookReply[0].length;j++){
output += lookReply[j][i];
}
output += "\n";
}
return output;
}
public boolean gameRunning(){
return active;
}
/**
* Quits the game when called
*/
public void quitGame() {
System.out.println("The game will now exit");
active = false;
}
/**
* finds a random position for the player in the map.
* #return Return null; if no position is found or a position vector [y,x]
*/
private int[] initiatePlayer() {
int[] pos = new int[2];
Random rand = new Random();
pos[0]=rand.nextInt(map.getMapHeight());
pos[1]=rand.nextInt(map.getMapWidth());
int counter = 1;
while (map.lookAtTile(pos[0], pos[1]) == '#' && counter < map.getMapHeight() * map.getMapWidth()) {
pos[1]= (int) ( counter * Math.cos(counter));
pos[0]=(int) ( counter * Math.sin(counter));
counter++;
}
return (map.lookAtTile(pos[0], pos[1]) == '#') ? null : pos;
}
/**
* checks if the player collected all GOLD and is on the exit tile
* #return True if all conditions are met, false otherwise
*/
protected boolean checkWin() {
if (collectedGold >= map.getWin() &&
map.lookAtTile(playerPosition[0], playerPosition[1]) == 'E') {
System.out.println("Congratulations!!! \n You have escaped the Dungeon of Dooom!!!!!! \n"
+ "Thank you for playing!");
return true;
}
return false;
}
public static void main(String [] args)
{
//int port = Integer.parseInt(args[0]);
int port = 40004;
try
{
//Thread t = new Server(port);
//t.start();
Server s = new Server(port);
s.startServer();
}catch(IOException e)
{
e.printStackTrace();
}
}
}
class ClientThread extends Thread {
//private byte[] data = new byte[255];
//private Socket socket;
private Socket _socket;
private Server _server;
public ClientThread(Socket socket, Server s) {
// for (int i = 0; i < data.length; i++)
// data[i] = (byte) i;
this._socket = socket;
this._server = s;
}
public void run() {
/* try {
OutputStream out = new BufferedOutputStream(socket.getOutputStream());
while (!socket.isClosed()) {
out.write(data);
}
socket.close();
} catch (Exception e) {
} */
try{
System.out.println("Just connected to " + _socket.getRemoteSocketAddress());
while ( _server.gameRunning() ) {
DataInputStream in = new DataInputStream(_socket.getInputStream());
// System.out.println(in.readUTF());
String input = in.readUTF();
System.out.println("Received from client: " + input );
DataOutputStream out = new DataOutputStream(_socket.getOutputStream());
//out.("Thank you for connecting to " + server.getLocalSocketAddress() + "\nGoodbye!");
if ( input.equals("LOOK") ) {
//String output = look();
System.out.println("LOOK" );
out.writeUTF( _server.look() );
} else if ( input.equals("HELLO") ) {
System.out.println("HELLO");
out.writeUTF( _server.hello() );
} else if ( input.equals("PICKUP") ) {
System.out.println("PICKUP");
out.writeUTF( _server.pickup() );
} else if ( input.equals("QUIT") ) {
System.out.println("QUIT");
_server.quitGame();
out.writeUTF( "The game has completed" );
} else if ( input.equals("MOVE N") ) {
System.out.println("MOVE N");
out.writeUTF( _server.move('N') );
} else if ( input.equals("MOVE E") ) {
System.out.println("MOVE E");
out.writeUTF( _server.move('E') );
} else if ( input.equals("MOVE S") ) {
System.out.println("MOVE S");
out.writeUTF( _server.move('S') );
} else if ( input.equals("MOVE W") ) {
System.out.println("MOVE W");
out.writeUTF( _server.move('W') );
} else {
out.writeUTF("Invalid Command");
}
}
}catch(IOException e){
//ioex.printStackTrace();
e.printStackTrace();
//break;
}
//DataOutputStream out = new DataOutputStream(server.getOutputStream());
//out.writeUTF("Thank you for connecting to " + server.getLocalSocketAddress() + "\nGoodbye!");
//server.close();
}
}

Server issues - Java

I'm trying to make a chat function with Java. The problem is that I have two classes. One for Client and one for ClientGUI. Where the Client one has the logic parts and the ClientGUI the design. The problem is getting is in row 46 where new ListenFromServer().start(); is getting a error
No enclosing instance of type Controller is accessible. Must
qualify the allocation with an enclosing instance of type
COntroller(e.g. x.new A() where x is an instance of Controller).
So what I did was that I changedpublic class ListenFromServer extends Thread to a static. Which means public static class ListenFromServer extends Thread and now the problem that I'm getting
Error connecting to the server: java.net.ConnectException: connect: Address is invalid on local machine, or port is not valid on remote machine
Controller (Client logic)
package Server;
import java.io.*;
import java.net.*;
import java.util.*;
public class Controller {
private static ObjectInputStream input;
private static ObjectOutputStream output;
private static Socket socket;
private static ClientGUI clientgui;
private static String username;
private static String server;
private static int port;
public static boolean startClient(){
try{
socket = new Socket(server, port);
}catch (Exception ex){
System.out.print("Error connecting to the server: " + ex);
return false;
}
String message = "Connection is accepted; " + socket.getInetAddress() +" - "+ socket.getPort();
System.out.println(message);
try {
input=new ObjectInputStream(socket.getInputStream());
output =new ObjectOutputStream(socket.getOutputStream());
}
catch (IOException io) {
System.out.print("Exception creating new Input/Output Stream: "+ io);
return false;
}
**********new ListenFromServer().start();********* //The problem is here
try {
output.writeObject(username);
}
catch(IOException io) {
System.out.print("Exception doing login: " + io);
disconnect();
return false;
}
return true;
}
private void display(String message) {
if(clientgui == null)
System.out.println(message);
else
clientgui.append(message +"\n");
}
public static void sendMessage(Message message) {
try {
output.writeObject(message);
}
catch(IOException exd) {
System.out.print("Eceptionwritingtoserver: " + exd);
}
}
private static void disconnect() {
try {
if(input != null)
input.close();
}catch (Exception ex){}
try{
if(output != null)
output.close();
}catch(Exception ex){}
try{
if(socket != null)
socket.close();
}catch(Exception ex){};
if (clientgui != null)
clientgui.connectionFailed();
}
public class ListenFromServer extends Thread{
public void run() {
while(true){
try{
String message = (String) input.readObject();
if(clientgui == null){
System.out.println(message);
System.out.print(":");
}
else {
clientgui.append(message);
}
}
catch(IOException io){
System.out.print("Server has closed the connection");
if(clientgui != null)
clientgui.connectionFailed();
break;
}
catch(ClassNotFoundException classex){
}
}
}
}
}
ClientGUI
package Server;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
/*
* The Client with its GUI
*/
public class ClientGUI extends JFrame implements ActionListener {
private static final long serialVersionUID = 1L;
private JLabel lblusername;
private JTextField textfieldusername, textfieldserver, textfieldportnumber;
private JButton btnlogin, btnlogout, btnonline;
private JTextArea textareamessage;
private boolean connected;
private Client client;
private int defaultPort;
private String defaultHost;
ClientGUI(String host, int port) {
super("Chat Client");
defaultPort = port;
defaultHost = host;
JPanel northPanel = new JPanel(new GridLayout(2,2));
JPanel serverAndPort = new JPanel(new GridLayout(1,2, 2, 2));
JLabel lblserveraddress = new JLabel("Server Address: ");
JLabel lblchat = new JLabel(" #BallIsLife");
JLabel lblportnumber = new JLabel("Port Number: ");
textfieldserver = new JTextField(host);
textfieldserver.setHorizontalAlignment(SwingConstants.LEFT);
textfieldserver.setFont(new Font("Tahoma", Font.PLAIN, 20));
textfieldportnumber = new JTextField("" + port);
textfieldportnumber.setFont(new Font("Tahoma", Font.PLAIN, 20));
textfieldportnumber.setHorizontalAlignment(SwingConstants.LEFT);
lblserveraddress.setFont(new Font("Tahoma", Font.PLAIN, 19));
serverAndPort.add(lblserveraddress);
serverAndPort.add(textfieldserver);
serverAndPort.add(lblchat);
serverAndPort.add(lblportnumber);
serverAndPort.add(textfieldportnumber);
lblchat.setForeground(Color.RED);
lblportnumber.setFont(new Font("Tahoma", Font.PLAIN, 19));
northPanel.add(serverAndPort);
getContentPane().add(northPanel, BorderLayout.NORTH);
JPanel panelbtn = new JPanel();
northPanel.add(panelbtn);
btnlogin = new JButton("Login");
panelbtn.add(btnlogin);
btnlogin.setFont(new Font("Tahoma", Font.PLAIN, 17));
btnlogin.addActionListener(this);
btnonline = new JButton("Online");
panelbtn.add(btnonline);
btnonline.setFont(new Font("Tahoma", Font.PLAIN, 17));
btnonline.addActionListener(this);
btnonline.setEnabled(false);
btnlogout = new JButton("Logout");
panelbtn.add(btnlogout);
btnlogout.setFont(new Font("Tahoma", Font.PLAIN, 17));
btnlogout.addActionListener(this);
btnlogout.setEnabled(false);
JButton btnPicture = new JButton("Picture");
btnPicture.setFont(new Font("Tahoma", Font.PLAIN, 17));
btnPicture.setEnabled(false);
panelbtn.add(btnPicture);
textareamessage = new JTextArea("Welcome to the #BallIsLife Chat room.\n");
textareamessage.setFont(new Font("Monospaced", Font.PLAIN, 15));
textareamessage.setLineWrap(true);
textareamessage.setEditable(false);
JPanel centerPanel = new JPanel(new GridLayout(1,1));
JScrollPane scrollPane = new JScrollPane(textareamessage);
centerPanel.add(scrollPane);
getContentPane().add(centerPanel, BorderLayout.CENTER);
JPanel southPanel = new JPanel();
getContentPane().add(southPanel, BorderLayout.SOUTH);
lblusername = new JLabel("Enter your username", SwingConstants.CENTER);
lblusername.setFont(new Font("Tahoma", Font.PLAIN, 15));
southPanel.add(lblusername);
textfieldusername = new JTextField("Write your username here.");
textfieldusername.setFont(new Font("Tahoma", Font.PLAIN, 14));
textfieldusername.setColumns(50);
southPanel.add(textfieldusername);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setSize(823, 665);
setVisible(true);
}
//Logiken
void append(String str) {
textareamessage.append(str);
textareamessage.setCaretPosition(textareamessage.getText().length() - 1);
}
void connectionFailed() {
btnlogin.setEnabled(true);
btnlogout.setEnabled(false);
btnonline.setEnabled(false);
lblusername.setText("Enter your username");
textfieldusername.setText("Write your username here");
textfieldportnumber.setText("" + defaultPort);
textfieldserver.setText(defaultHost);
textfieldserver.setEditable(false);
textfieldportnumber.setEditable(false);
textfieldusername.removeActionListener(this);
connected = false;
}
//
public void actionPerformed(ActionEvent e) {
Object button = e.getSource();
if(button == btnlogout) {
Controller.sendMessage(new Message("", Message.LOGOUT)); //Ändra till Chatmessage klass
btnlogin.setText("Login");
return;
}
if(button == btnonline) {
Controller.sendMessage(new Message("", Message.ONLINE)); //Ändra till Chatmessage klass
return;
}
if(connected) {
Controller.sendMessage(new Message(textfieldusername.getText(), Message.MESSAGE)); //Ändra till Chatmessage klass
textfieldusername.setText("");
return;
}
if(button == btnlogin) {
String username = textfieldusername.getText();
if(username.length() == 0)
return;
String server = textfieldserver.getText();
if(server.length() == 0)
return;
String portNumber = textfieldportnumber.getText();
if(portNumber.length() == 0)
return;
int port = 0;
try {
port = Integer.parseInt(portNumber);
}
catch(Exception en) {
return;
}
client = new Client(server, username, port, this);
if(!Controller.startClient())
return;
}
connected = true;
textfieldusername.setText("");
btnlogin.setText("Send message");
btnlogin.setEnabled(true);
btnlogout.setEnabled(true);
btnonline.setEnabled(true);
textfieldserver.setEditable(false);
textfieldportnumber.setEditable(false);
textfieldusername.addActionListener(this);
}
// to start the whole thing the server
public static void main(String[] args) {
new ClientGUI("localhost", 1500);
}
}
Server
package Server;
import java.io.*;
import java.net.*;
import java.text.SimpleDateFormat;
import java.util.*;
/*
* The server that can be run both as a console application or a GUI
*/
public class Server {
// a unique ID for each connection
private static int uniqueId;
// an ArrayList to keep the list of the Client
private ArrayList<ClientThread> al;
// if I am in a GUI
private ServerGUI sg;
// to display time
private SimpleDateFormat sdf;
// the port number to listen for connection
private int port;
// the boolean that will be turned of to stop the server
private boolean keepGoing;
/*
* server constructor that receive the port to listen to for connection as parameter
* in console
*/
public Server(int port) {
this(port, null);
}
public Server(int port, ServerGUI sg) {
// GUI or not
this.sg = sg;
// the port
this.port = port;
// to display hh:mm:ss
sdf = new SimpleDateFormat("HH:mm:ss");
// ArrayList for the Client list
al = new ArrayList<ClientThread>();
}
public void start() {
keepGoing = true;
/* create socket server and wait for connection requests */
try
{
// the socket used by the server
ServerSocket serverSocket = new ServerSocket(port);
// infinite loop to wait for connections
while(keepGoing)
{
// format message saying we are waiting
display("Server waiting for Clients on port " + port + ".");
Socket socket = serverSocket.accept(); // accept connection
// if I was asked to stop
if(!keepGoing)
break;
ClientThread t = new ClientThread(socket); // make a thread of it
al.add(t); // save it in the ArrayList
t.start();
}
// I was asked to stop
try {
serverSocket.close();
for(int i = 0; i < al.size(); ++i) {
ClientThread tc = al.get(i);
try {
tc.sInput.close();
tc.sOutput.close();
tc.socket.close();
}
catch(IOException ioE) {
// not much I can do
}
}
}
catch(Exception e) {
display("Exception closing the server and clients: " + e);
}
}
// something went bad
catch (IOException e) {
String msg = sdf.format(new Date()) + " Exception on new ServerSocket: " + e + "\n";
display(msg);
}
}
/*
* For the GUI to stop the server
*/
protected void stop() {
keepGoing = false;
// connect to myself as Client to exit statement
// Socket socket = serverSocket.accept();
try {
new Socket("localhost", port);
}
catch(Exception e) {
// nothing I can really do
}
}
/*
* Display an event (not a message) to the console or the GUI
*/
private void display(String msg) {
String time = sdf.format(new Date()) + " " + msg;
if(sg == null)
System.out.println(time);
else
sg.appendRoom(null,time + "\n");
}
/*
* to broadcast a message to all Clients
*/
private synchronized void broadcast(String message) {
// add HH:mm:ss and \n to the message
String time = sdf.format(new Date());
String messageLf = time + " " + message + "\n";
// display message on console or GUI
if(sg == null)
System.out.print(messageLf);
else
sg.appendRoom(messageLf,null); // append in the room window
// we loop in reverse order in case we would have to remove a Client
// because it has disconnected
for(int i = al.size(); --i >= 0;) {
ClientThread ct = al.get(i);
// try to write to the Client if it fails remove it from the list
if(!ct.writeMsg(messageLf)) {
al.remove(i);
display("Disconnected Client " + ct.username + " removed from list.");
}
}
}
// for a client who logoff using the LOGOUT message
synchronized void remove(int id) {
// scan the array list until we found the Id
for(int i = 0; i < al.size(); ++i) {
ClientThread ct = al.get(i);
// found it
if(ct.id == id) {
al.remove(i);
return;
}
}
}
/*
* To run as a console application just open a console window and:
* > java Server
* > java Server portNumber
* If the port number is not specified 1500 is used
*/
public static void main(String[] args) {
// start server on port 1500 unless a PortNumber is specified
int portNumber = 1500;
switch(args.length) {
case 1:
try {
portNumber = Integer.parseInt(args[0]);
}
catch(Exception e) {
System.out.println("Invalid port number.");
System.out.println("Usage is: > java Server [portNumber]");
return;
}
case 0:
break;
default:
System.out.println("Usage is: > java Server [portNumber]");
return;
}
// create a server object and start it
Server server = new Server(portNumber);
server.start();
}
/** One instance of this thread will run for each client */
class ClientThread extends Thread {
// the socket where to listen/talk
Socket socket;
ObjectInputStream sInput;
ObjectOutputStream sOutput;
// my unique id (easier for deconnection)
int id;
// the Username of the Client
String username;
// the only type of message a will receive
Message cm;
// the date I connect
String date;
// Constructore
ClientThread(Socket socket) {
// a unique id
id = ++uniqueId;
this.socket = socket;
/* Creating both Data Stream */
System.out.println("Thread trying to create Object Input/Output Streams");
try
{
// create output first
sOutput = new ObjectOutputStream(socket.getOutputStream());
sInput = new ObjectInputStream(socket.getInputStream());
// read the username
username = (String) sInput.readObject();
display(username + " just connected.");
}
catch (IOException e) {
display("Exception creating new Input/output Streams: " + e);
return;
}
// have to catch ClassNotFoundException
// but I read a String, I am sure it will work
catch (ClassNotFoundException e) {
}
date = new Date().toString() + "\n";
}
// what will run forever
public void run() {
// to loop until LOGOUT
boolean keepGoing = true;
while(keepGoing) {
// read a String (which is an object)
try {
cm = (Message) sInput.readObject();
}
catch (IOException e) {
display(username + " Exception reading Streams: " + e);
break;
}
catch(ClassNotFoundException e2) {
break;
}
// the messaage part of the ChatMessage
String message = cm.getMessage();
// Switch on the type of message receive
switch(cm.getType()) {
case Message.MESSAGE:
broadcast(username + ": " + message);
break;
case Message.LOGOUT:
display(username + " disconnected with a LOGOUT message.");
keepGoing = false;
break;
case Message.ONLINE:
writeMsg("List of the users connected at " + sdf.format(new Date()) + "\n");
// scan al the users connected
for(int i = 0; i < al.size(); ++i) {
ClientThread ct = al.get(i);
writeMsg((i+1) + ") " + ct.username + " since " + ct.date);
}
break;
}
}
// remove myself from the arrayList containing the list of the
// connected Clients
remove(id);
close();
}
// try to close everything
private void close() {
// try to close the connection
try {
if(sOutput != null) sOutput.close();
}
catch(Exception e) {}
try {
if(sInput != null) sInput.close();
}
catch(Exception e) {};
try {
if(socket != null) socket.close();
}
catch (Exception e) {}
}
/*
* Write a String to the Client output stream
*/
private boolean writeMsg(String msg) {
// if Client is still connected send the message to it
if(!socket.isConnected()) {
close();
return false;
}
// write the message to the stream
try {
sOutput.writeObject(msg);
}
// if an error occurs, do not abort just inform the user
catch(IOException e) {
display("Error sending message to " + username);
display(e.toString());
}
return true;
}
}
}
ServerGUI
package Server;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
/*
* The server as a GUI
*/
public class ServerGUI extends JFrame implements ActionListener, WindowListener {
private static final long serialVersionUID = 1L;
// the stop and start buttons
private JButton stopStart, saveLog;
// JTextArea for the chat room and the events
private JTextArea chat;
// private JTextArea event;
// The port number
private JTextField tfPortNumber;
// my server
private Server server;
// server constructor that receive the port to listen to for connection as
// parameter
ServerGUI(int port) {
super("Chat Server");
server = null;
// in the NorthPanel the PortNumber the Start and Stop buttons
JPanel north = new JPanel();
north.add(new JLabel("Port number: "));
tfPortNumber = new JTextField("" + port);
north.add(tfPortNumber);
// to stop or start the server, we start with "Start"
stopStart = new JButton("Start");
stopStart.addActionListener(this);
saveLog = new JButton("Save log");
saveLog.addActionListener(this);
north.add(stopStart);
north.add(saveLog);
add(north, BorderLayout.NORTH);
// the event and chat room
JPanel center = new JPanel(new GridLayout());
chat = new JTextArea(120, 20);
chat.setEditable(false);
chat.setWrapStyleWord(true);
chat.setLineWrap(true);
appendRoom(null, "Chat room and Events log for server.\n");
center.add(new JScrollPane(chat));
// event = new JTextArea(80,80);
// event.setEditable(false);
// appendEvent("Events log.\n");
// center.add(new JScrollPane(event));
add(center);
// need to be informed when the user click the close button on the frame
addWindowListener(this);
setSize(450, 600);
setVisible(true);
}
public void writeLog() {
try {
JFileChooser chooser = new JFileChooser();
String content = chat.getText();
int actionDialog = chooser.showSaveDialog(this);
content = content.replaceAll("(?!\\r)\\n", "\r\n");
if (actionDialog == JFileChooser.APPROVE_OPTION) {
File file = new File(chooser.getSelectedFile() + ".txt");
// if file doesnt exists, then create it
// if (!file.exists()) {
// file.createNewFile();
// }
FileWriter fw = new FileWriter(file.getAbsoluteFile());
BufferedWriter bw = new BufferedWriter(fw);
bw.write(content);
bw.close();
}
} catch (IOException ee) {
ee.printStackTrace();
}
// JFileChooser chooser = new JFileChooser();
// // chooser.setCurrentDirectory(new File("./"));
// int actionDialog = chooser.showSaveDialog(this);
// if (actionDialog == JFileChooser.APPROVE_OPTION) {
// File fileName = new File(chooser.getSelectedFile() + "");
// if (fileName == null)
// // return;
// if (fileName.exists()) {
// actionDialog = JOptionPane.showConfirmDialog(this,
// "Replace existing file?");
// if (actionDialog == JOptionPane.NO_OPTION)
// return;
// }
// try (BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(
// new FileOutputStream(fileName), "ISO-8859-1"))) {
// bw.write(saveText);
// // bw.newLine();
// bw.flush();
// }
// }
}
// append message to the two JTextArea
// position at the end
void appendRoom(String chatStr, String eventStr) {
chat.append(chatStr);
chat.append(eventStr);
// chat.setCaretPosition(chat.getText().length() - 1);
}
// void appendEvent(String str) {
// event.append(str);
// event.setCaretPosition(chat.getText().length() - 1);
//
// }
// start or stop where clicked
public void actionPerformed(ActionEvent e) {
// if running we have to stop
if (e.getSource() == saveLog) {
writeLog();
} else if (e.getSource() == stopStart) {
if (server != null) {
server.stop();
server = null;
tfPortNumber.setEditable(true);
stopStart.setText("Start");
return;
}
// OK start the server
int port;
try {
port = Integer.parseInt(tfPortNumber.getText().trim());
} catch (Exception er) {
appendRoom(null, "Invalid port number");
return;
}
// ceate a new Server
server = new Server(port, this);
// and start it as a thread
new ServerRunning().start();
stopStart.setText("Stop");
tfPortNumber.setEditable(false);
}
}
/*
* A thread to run the Server
*/
class ServerRunning extends Thread {
public void run() {
server.start(); // should execute until if fails
// the server failed
stopStart.setText("Start");
tfPortNumber.setEditable(true);
appendRoom(null, "Server closed\n");
server = null;
}
}
// entry point to start the Server
public static void main(String[] arg) {
// start server default port 1500
new ServerGUI(1500);
}
/*
* If the user click the X button to close the application I need to close
* the connection with the server to free the port
*/
public void windowClosing(WindowEvent e) {
// if my Server exist
if (server != null) {
try {
server.stop(); // ask the server to close the conection
} catch (Exception eClose) {
}
server = null;
}
// dispose the frame
dispose();
System.exit(0);
}
// I can ignore the other WindowListener method
public void windowClosed(WindowEvent e) {
}
public void windowOpened(WindowEvent e) {
}
public void windowIconified(WindowEvent e) {
}
public void windowDeiconified(WindowEvent e) {
}
public void windowActivated(WindowEvent e) {
}
public void windowDeactivated(WindowEvent e) {
}
// /*
// * A thread to run the Server
// */
// class ServerRunning extends Thread {
// public void run() {
// server.start(); // should execute until if fails
// // the server failed
// stopStart.setText("Start");
// tfPortNumber.setEditable(true);
// appendRoom(null, "Server closed\n");
// server = null;
// }
// }
}
Message
package Server;
import java.io.Serializable;
//Klassen som kollar vad för typ av message
public class Message implements Serializable {
protected static final long serialVersionUID = 42L;
static final int ONLINE = 0;
static final int MESSAGE = 1;
static final int LOGOUT = 2;
private int SAVELOG = 3;
private String message;
private int type;
public Message(String message, int type){
this.type = type;
this.message = message;
}
public int getType(){
return type;
}
public String getMessage(){
return message;
}
}
Make your inner class ListenFromServer static, since you are referring to it from a static method
public class Controller {
...
public static class ListenFromServer {
...
}
}

Categories