Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.net.*;
- import java.io.*;
- class Server {
- public static ServerSocket servSock;
- public static Socket s = new Socket();
- public static BufferedReader input;
- public static PrintWriter printwriter ;
- public static void main(String []args)
- {
- try
- {
- awaitConnection();
- setupReader();
- }
- catch(Exception e)
- {
- System.out.println(e.getMessage());
- }
- }
- private static void awaitConnection() throws IOException {
- ServerSocket servSock = new ServerSocket(8080);
- System.out.println("Awaiting connection...");
- s = servSock.accept();
- InetAddress obj = s.getInetAddress();
- System.out.println("Connection established with " + obj);
- }
- private static void setupReader() throws IOException {
- while(true) {
- input = new BufferedReader(new InputStreamReader(s.getInputStream()));
- String msg = input.readLine();
- if(msg.contains("bye")) {
- break;
- }
- else {
- printwriter.println("this is a message from the server");
- }
- printwriter.flush();
- }
- s.close();
- }
- }
- import java.net.*;
- import java.io.*;
- import java.util.*;
- class Client
- {
- static Socket clientSocket = null;
- static InetAddress obj = null;
- static PrintWriter output;
- static String message;
- static BufferedReader br;
- public static void main(String []args)
- {
- try
- {
- obj = InetAddress.getByName(args[0]);
- connect();
- send();
- }
- catch(Exception e)
- {
- System.out.println(e.getMessage());
- }
- }
- private static void connect() throws IOException {
- System.out.println("Trying to connect...");
- clientSocket = new Socket(obj, 8080);
- System.out.println("Your are connected to the server! nEnter Message:");
- }
- private static void send() throws IOException {
- while(true) {
- System.out.println(br.readLine());
- output = new PrintWriter(clientSocket.getOutputStream(), true);
- message = new Scanner(System.in).nextLine();
- output.println(message);
- if(message.contains("bye"))
- {
- break;
- }
- }
- output.close();
- clientSocket.close();
- }
- }
Add Comment
Please, Sign In to add comment