Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- BufferedReader BufReader1 = new BufferedReader(new InputStreamReader(socket.getInputStream(), "UTF-8"));
- String MessageFromClient = BufReader1.readLine();
- import java.io.*;
- import java.net.*;
- import java.util.*;
- import static java.nio.charset.StandardCharsets.*;
- public class ChatServer
- {
- ChatServer chatserver = new ChatServer();
- private static Socket socket;
- public static void main(String args[])
- {
- Thread ChatServer1 = new Thread ()
- {
- public void run ()
- {
- System.out.println("Server thread is now running");
- try
- {
- int port_number1 = 0;
- int numberOfClients = 0;
- boolean KeepRunning = true;
- if(args.length>0)
- {
- port_number1 = Integer.valueOf(args[0]);
- }
- System.out.println("Waiting for connections on port " + port_number1);
- try
- {
- ServerSocket serverSocket = new ServerSocket(port_number1);
- socket = serverSocket.accept();
- }
- catch (IOException e)
- {
- e.printStackTrace();
- }
- System.out.println( "Listening for connections on port: " + ( port_number1 ) );
- while(KeepRunning)
- {
- //create a list of clients
- ArrayList<String> ListOfClients = new ArrayList<String>();
- //connect to client
- // socket = serverSocket.accept();
- //add new client to the list, is this the right way to add a new client? or should it be in a for loop or something?
- ListOfClients.add("new client");
- numberOfClients += 1;
- System.out.println("A client has connected. Waiting for message...");
- ListOfClients.add("new client" + numberOfClients);
- //reading encoded utf-8 message from client, decoding from utf-8 format
- BufferedReader BufReader1 = new BufferedReader(new InputStreamReader(socket.getInputStream(), "UTF-8"));
- String MessageFromClient = BufReader1.readLine();
- // System.out.println("The message is currently encoded UTF-8");
- //byte[] bytes = MessageFromClientDecodedFromUTF8.getBytes("UTF-8");
- // String MessageFromClientDecoded = new String(bytes, "UTF-8");
- System.out.println("Message received from client (decoded utf-8): "+ MessageFromClient);
- //relaying message to every other client besides the one it was from
- //
- // for (int i = 0; i < ListOfClients.size(); i++)
- // {
- //if(ListOfClients.get(i)!="new client")
- //{
- String newmessage = null;
- String returnMessage = newmessage;
- OutputStream os = socket.getOutputStream();
- OutputStreamWriter osw = new OutputStreamWriter(os);
- BufferedWriter bw = new BufferedWriter(osw);
- if(returnMessage != null)
- {
- bw.write(returnMessage + "n");
- System.out.println("Message sent to all other clients: "+returnMessage);
- }
- bw.flush();
- bw.close();
- // out.close();
- os.close();
- //}
- //}
- }
- }
- catch (IOException e)
- {
- e.printStackTrace();
- }
- finally
- {
- try
- {
- if (socket != null)
- {
- socket.close ();
- }
- }
- catch (IOException e)
- {
- e.printStackTrace();
- }
- }
- }
- };
- ChatServer1.start();
- }
- }
- import java.io.*;
- import java.net.*;
- import java.util.*;
- import static java.nio.charset.StandardCharsets.*;
- public class ChatClient
- {
- final int numberOfClients = 0;
- public static void main(String args[])
- {
- ChatClient chatclient = new ChatClient();
- //If I wanted to create multiple clients, would this code go here? OR should the new thread creation be outside the while(true) loop?
- String host = "localhost";
- int numberOfClients = 0;
- Thread ChatClient1 = new Thread ()
- {
- public void run()
- {
- boolean KeepRunning = true;
- while(KeepRunning == true && args.length > 0)
- {
- try
- {
- //Client begins, gets port number, listens, connects, prints out messages from other clients
- int port = 0;
- int port_1number1 = 0;
- int numberofmessages = 0;
- System.out.println("Try block begins..");
- System.out.println("Chat client is running");
- System.out.println("Port number is: " + args[0]);
- if(args.length>0)
- {
- port = Integer.valueOf(args[0]);
- }
- System.out.println("Listening for connections..");
- System.out.println( "Listening on port: " + args[0] );
- // System.out.println(messagessentbyotherclients);
- try
- {
- Socket clientSocket = new Socket("localhost", port);
- InetAddress inetlocalhost = InetAddress.getByName("localhost");
- SocketAddress localhost = new InetSocketAddress(inetlocalhost, port);
- clientSocket.connect(localhost, port);
- System.out.println("Client has connected");
- //client creates new message from standard input
- OutputStream os = clientSocket.getOutputStream();
- OutputStreamWriter osw = new OutputStreamWriter(os);
- BufferedWriter bw = new BufferedWriter(osw);
- }
- catch (IOException e)
- {
- e.printStackTrace();
- }
- //creating message to send from standard input
- String newmessage = "";
- try
- {
- int k = 3;
- if(k==3)
- {
- // input the message from standard input encoded in UTF-8 string format
- BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
- String line = "";
- System.out.println( "Standard input (press enter then control D when finished): " );
- while( (line= input.readLine()) != null && k==3 )
- {
- newmessage += line + " ";
- k=4;
- }
- k=6;
- }
- }
- catch ( Exception e )
- {
- System.out.println( e.getMessage() );
- }
- //Sending the message to server
- String sendMessage = newmessage;
- try
- {
- Socket clientSocket = new Socket("localhost", port);
- InetAddress addr = InetAddress.getByName("localhost");
- SocketAddress sockaddr = new InetSocketAddress(addr, port);
- OutputStream os = clientSocket.getOutputStream();
- OutputStreamWriter osw = new OutputStreamWriter(os);
- BufferedWriter bw = new BufferedWriter(osw);
- bw.write(sendMessage + "n");
- bw.flush();
- bw.close();
- //out.close();
- os.close();
- }
- catch (IOException e)
- {
- e.printStackTrace();
- }
- System.out.println("Message sent to server: "+sendMessage);
- }
- finally
- {
- }
- KeepRunning = false;
- }
- }
- };
- ChatClient1.start();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement