Socket achieve simple chat room, Client, Server

package seday08;

import java.io.BufferedWriter;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.Scanner;

/ **
* @author xingsir
* simple chat room client
* /
public class Client {
/ *
* socket the java.net.Socket
* the Socket encapsulates details of the underlying TCP communication protocol, it can be used by
* TCP protocols and services end of the link is established, and data IO operation is completed and server to exchange two flow
* /
Private Socket Socket; // declare an object of type Socket
// Constructors
public Client () {
the try {

System.out.println ( "Connecting to server ......");
socket = new new the Socket ( "localhost", 33069); // port range 0-65535
System.out.println ( "has been successfully connected to the server ! ");
} the catch (Exception E) {
e.printStackTrace (); // stack output error information to help locate and resolve errors
}
}
/ *
* method client begins to work
* /
public void start () {

{the try
the OutputStream Socket.getOutputStream OUT = (); the getOutputStream methods // Socket object client is actually obtained output stream data transmitted to the server.
OutputStreamWriter osw = new OutputStreamWriter (out) ; // switch character byte stream
BufferedWriter bw = new BufferedWriter (osw) ; // buffer stream
PrintWriter pw = new PrintWriter (bw, true); // row update
Scanner scanner = new Scanner ( System.in); // console input, keyboard input information acquired
while (true) {// condition is true
String message scanner.nextLine = ();
pw.println (message);
}
} the catch (Exception E) {
E .printStackTrace (); // stack output error information to help locate and resolve errors
}
}

public static void main (String [] args) {
// instantiate a client
client client = new new client ();
// call starting method
client.start ();

}

}

//===================================================================================

package seday08;

java.io.BufferedReader Import;
Import java.io.IOException;
Import a java.io.InputStream;
Import the java.io.InputStreamReader;
Import the java.net.ServerSocket;
Import the java.net.Socket;
/ **
* @author xingsir
* server
* /
public class server {
Private ServerSocket server; // declare an object of type ServerSocket
// constructor
public server () {
the try {
System.out.println ( "server starting ......") ;
Server = new new the ServerSocket (33069); // port number received
System.out.println ( "server boot is completed!");
} the catch (IOException E) {
e.printStackTrace (); // stack output error message, help to locate and resolve errors
}
}
// server working methods
public void Start () {
the try {
the while (to true) {
System.out.println ( "connection requests from clients ......");
the Socket Socket server.accept = (); // Accept (): accept the connection request from the client, and returns a sleeve Sockets
System.out.println; ( "a client linked!")
/ *
* Socket method provided: the getInputStream the InputStream ()
* Socket acquired by the input byte stream read from the remote computer is sent from byte.
* /
The InputStream in Socket.getInputStream = ();
// byte character converting
the InputStreamReader new new ISR = the InputStreamReader (in, "UTF-. 8");
the BufferedReader br = new new the BufferedReader (ISR); // block read, and may be reads a string row

String message = null; // declaration string Message, initializes an empty
/ *
* sent from the terminal via the client line of the string BufferedReader reading this operation, when the client disconnects,
* because the client systems, server performance here is not the same:
* when the windows client disconnects, readLine method throws an exception directly
when * linux client disconnects, readLine method returns is null
* /
the while ((the Message = br.readLine ( !)) = null) {
System.out.println ( "message from the client, said:" + message); // Print message console
}

}
} The catch (Exception E) {
e.printStackTrace (); // stack output error information to help locate and resolve errors
}
}

static void main public (String [] args) {
// instantiates a server-side
Server server = new new Server ();
server.start (); // call the method
}

}

Guess you like

Origin www.cnblogs.com/xingsir/p/12053174.html