- import java.net.*;
- import java.io.*;
- class Client {
- public static void main(String args[]) throws IOException {
- Socket soc = null;
- String str = null;
- BufferedReader br = null;
- DataOutputStream dos = null;
- BufferedReader kyrd = new BufferedReader(new InputStreamReader(System.in));
- try {
- soc = new Socket(InetAddress.getLocalHost(), 95);
- br = new BufferedReader(new InputStreamReader(soc.getInputStream()));
- dos = new DataOutputStream(soc.getOutputStream());
- } catch (UnknownHostException uhe) {
- System.out.println("Unknown Host");
- System.exit(0);
- }
- System.out.println("To start the dialog type the message in this client window \n Type exit to end");
- boolean more = true;
- while (more) {
- str = kyrd.readLine();
- dos.writeBytes(str);
- dos.write(13);
- dos.write(10);
- dos.flush();
- String s, s1;
- s = br.readLine();
- System.out.println("From server :" + s);
- if (s.equals("exit")) {
- break;
- }
- }
- br.close();
- dos.close();
- soc.close();
- }
- }
- import java.net.*;
- import java.io.*;
- class Server {
- public static void main(String args[]) throws IOException {
- ServerSocket ss = null;
- try {
- ss = new ServerSocket(95);
- } catch (IOException ioe) {
- System.out.println("Error finding port");
- System.exit(1);
- }
- Socket soc = null;
- try {
- soc = ss.accept();
- System.out.println("Connection accepted at :" + soc);
- } catch (IOException ioe) {
- System.out.println("Server failed to accept");
- System.exit(1);
- }
- DataOutputStream dos = new DataOutputStream(soc.getOutputStream());
- BufferedReader br = new BufferedReader(new InputStreamReader(soc.getInputStream()));
- String s;
- System.out.println("Server waiting for message from tthe client");
- boolean quit = false;
- do {
- String msg = "";
- s = br.readLine();
- int len = s.length();
- if (s.equals("exit")) {
- quit = true;
- }
- for (int i = 0; i < len; i++) {
- msg = msg + s.charAt(i);
- dos.write((byte) s.charAt(i));
- }
- System.out.println("From client :" + msg);
- dos.write(13);
- dos.write(10);
- dos.flush();
- } while (!quit);
- dos.close();
- soc.close();
- }
- }