Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package Client;
- import java.io.DataInputStream;
- import java.io.IOException;
- import java.io.PrintStream;
- import java.net.ServerSocket;
- import java.net.Socket;
- public class TCPserver {
- public final int port = 5000;
- private String datas ;
- private ServerSocket theServer;
- private Socket theConnection;
- private PrintStream p;
- //CÓ DÙNG THÊM CÁI NÀY
- DataInputStream dataInputStream = null;
- ////////////////////////////////
- public TCPserver(String data){
- this.datas=data;
- }
- public void wakeUp() {
- System.out.println("Server started...");
- try {
- theServer = new ServerSocket(port);
- theConnection = theServer.accept();
- /*
- * VÀ NHẬN MESSAGE Ở ĐÂY
- *
- *
- * */
- dataInputStream = new DataInputStream(
- theConnection.getInputStream());
- String messageFromClient = "";
- //If no message sent from client, this code will block the program
- messageFromClient = dataInputStream.readUTF();
- System.out.println(messageFromClient);
- /*
- *
- *
- * */
- p = new PrintStream(theConnection.getOutputStream());
- //p.println(datas);
- //System.out.println(datas);
- datas = "Data 2";
- p.println(datas);
- System.out.println(datas);
- theConnection.close();
- theServer.close();
- }
- catch (IOException e) {
- System.err.println(e);
- }
- }
- public void main(String arg[]) throws Exception {
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement