Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Server
- package server;
- import java.io.DataInputStream;
- import java.io.DataOutputStream;
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.OutputStream;
- import java.net.ServerSocket;
- import java.net.Socket;
- import java.net.SocketTimeoutException;
- import java.util.Scanner;
- //import java.net.*;
- //import java.io.*;
- public class Server extends Thread {
- private ServerSocket serverSocket;
- private Socket socket;
- private InputStream inFromClient;
- private DataInputStream in;
- private OutputStream outToClient;
- private DataOutputStream out;
- public Server(int port) throws IOException {
- //Starting server
- serverSocket = new ServerSocket(port);
- System.out.println("Server Started and listening");
- serverSocket.setSoTimeout(60000); //60 Sec
- }
- public void run() {
- while (true) {
- try {
- //Accepting from the client
- socket = serverSocket.accept();
- //Getting the message from the client (read)
- inFromClient = socket.getInputStream();
- //in = new DataInputStream(socket.getInputStream());
- in = new DataInputStream(inFromClient);
- System.out.println("Client says; " + in.readUTF());
- //Sending the message to the client (write)
- outToClient = socket.getOutputStream();
- out = new DataOutputStream(socket.getOutputStream());
- //out = new DataOutputStream(outToClient);
- //out.writeUTF("Thank you for connecting to " + socket.getLocalSocketAddress());
- Scanner scn = new Scanner(System.in);
- String input = scn.nextLine();
- out.writeUTF(input);
- //Releasing memory
- socket.close();
- } catch (SocketTimeoutException s) {
- System.out.println("Socket timed out!");
- break;
- } catch (IOException e) {
- e.printStackTrace();
- break;
- }
- }
- }
- public static void main(String[] args) {
- //int port = Integer.parseInt(args[0]);
- int port = 8080;
- try {
- Thread t = new Server(port);
- t.start();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
- // Client
- package client;
- import java.io.DataInputStream;
- import java.io.DataOutputStream;
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.OutputStream;
- import java.net.Socket;
- import java.util.Scanner;
- //import java.net.*;
- //import java.io.*;
- public class Client {
- private static Socket socket;
- public static void main(String[] args) {
- // String host = args[0];
- // int port = Integer.parseInt(args[1]);
- String host = "localhost";
- int port = 8080;
- try {
- while (true) {
- //Connecting to Server
- socket = new Socket(host, port);
- //Sending the message to the server (write)
- OutputStream outToServer = socket.getOutputStream();
- DataOutputStream out = new DataOutputStream(outToServer);
- //out.writeUTF("Hello from " + socket.getLocalSocketAddress());
- Scanner scn = new Scanner(System.in);
- String input = scn.nextLine();
- out.writeUTF(input);
- //Getting the message from the sever (read)
- InputStream inFromServer = socket.getInputStream();
- DataInputStream in = new DataInputStream(inFromServer);
- System.out.println("Server says; " + in.readUTF());
- //Releasing memory
- socket.close();
- }
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment