Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package TCP;
- import java.io.IOException;
- import java.net.ServerSocket;
- import java.net.Socket;
- import java.net.UnknownHostException;
- import java.util.Scanner;
- public class ConsoleChat {
- private ServerSocket providerSocket;
- private Socket connection;
- private Socket requestSocket;
- void run() {
- try {
- providerSocket = new ServerSocket(6666);
- Thread serverWait = new Thread() {
- @Override
- public void run() {
- try {
- System.out.println("Venter pƄ forbindelse");
- connection = providerSocket.accept();
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- };
- Thread clientWait = new Thread() {
- @Override
- public void run() {
- Scanner in = new Scanner(System.in);
- System.out.print("Input server id: ");
- String ip = in.nextLine();
- System.out.println("Forbinder til " + ip);
- try {
- requestSocket = new Socket(ip, 6666);
- System.out.println("Har lavet request");
- } catch (UnknownHostException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- };
- clientWait.start();
- serverWait.start();
- while (connection == null && requestSocket == null) {
- ;
- }
- if (connection != null) {
- clientWait.interrupt();
- System.out.println("Forbindelse fra "
- + connection.getInetAddress().getHostAddress());
- } else {
- serverWait.interrupt();
- }
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- public static void main(String args[]) {
- ConsoleChat klient = new ConsoleChat();
- klient.run();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement