Home > Back-end >  TCP to obtain client input stream
TCP to obtain client input stream

Time:09-27

The client
The console output to 2 only, not to perform the

 package com. Yqx. TCP; 

Import the Java. IO. BufferedInputStream;
Import the Java. IO. BufferedOutputStream;
import java.io.BufferedReader;
Import the Java. IO. DataOutputStream;
import java.io.IOException;
Import the Java. IO. InputStreamReader;
Import the Java. IO. ObjectInputStream;
import java.io.ObjectOutputStream;
The import java.net.Socket;
import java.net.UnknownHostException;

Public class LoginClient {

Public static void main (String [] args) throws UnknownHostException, IOException {
//TODO Auto - generated method stub
The Socket client=new Socket (" localhost ", 1111);
System. The out. Println (1);
The ObjectOutputStream oos=new ObjectOutputStream (new BufferedOutputStream (client) getOutputStream ()));
System. The out. Println (2);
//InputSteam!!!!!!!!!!
ObjectInputStream ois=new ObjectInputStream (new BufferedInputStream (client) getInputStream ()));
System. The out. Println (3);
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System. The out. Println (" please enter the user name ");
String name=br. ReadLine ();
System. The out. Println (" please enter the password ");
String PWD=br. ReadLine ();
Oos. WriteObject (new User (name, PWD));
Oos. The flush ();

String MSG=ois. ReadUTF ();
System. The out. Println (MSG);
Ois. Close ();
client.close();
}

}


Server

 package com. Yqx. TCP; 

Import the Java. IO. BufferedInputStream;
Import the Java. IO. BufferedOutputStream;
Import the Java. IO. A DataInputStream;
import java.io.IOException;
Import the Java. IO. ObjectInputStream;
import java.io.ObjectOutputStream;
The import java.net.ServerSocket;
The import java.net.Socket;

Public class LoginServer {

Public static void main (String [] args) throws IOException, ClassNotFoundException {
ServerSocket server=new ServerSocket (1111);
The Socket client=server. The accept ();
ObjectInputStream ois=new ObjectInputStream (new BufferedInputStream (client) getInputStream ()));
The ObjectOutputStream oos=new ObjectOutputStream (new BufferedOutputStream (client) getOutputStream ()));
The User u=(User) ois. ReadObject ();
System. The out. Println (u);
If (u.g etName (.) the equals (" yqx ") & amp; & U.g etPassword (.) the equals (" 123 ")) {
Oos. WriteUTF (" login successful!" );
}
The else {
Oos. WriteUTF (" account or password error!" );
}
Oos. The close ();
client.close();
}

}


And when my client to get behind the input stream can run normally

Code doesn't change, just changed the client. The getInputStream () the location of the

 package com. Yqx. TCP; 

Import the Java. IO. BufferedInputStream;
Import the Java. IO. BufferedOutputStream;
import java.io.BufferedReader;
Import the Java. IO. DataOutputStream;
import java.io.IOException;
Import the Java. IO. InputStreamReader;
Import the Java. IO. ObjectInputStream;
import java.io.ObjectOutputStream;
The import java.net.Socket;
import java.net.UnknownHostException;

Public class LoginClient {

Public static void main (String [] args) throws UnknownHostException, IOException {
//TODO Auto - generated method stub
The Socket client=new Socket (" localhost ", 1111);
The ObjectOutputStream oos=new ObjectOutputStream (new BufferedOutputStream (client) getOutputStream ()));
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System. The out. Println (" please enter the user name ");
String name=br. ReadLine ();
System. The out. Println (" please enter the password ");
String PWD=br. ReadLine ();
Oos. WriteObject (new User (name, PWD));
Oos. The flush ();
//InputSteam!!!!!!!!!!
ObjectInputStream ois=new ObjectInputStream (new BufferedInputStream (client) getInputStream ()));
String MSG=ois. ReadUTF ();
System. The out. Println (MSG);
Ois. Close ();
client.close();
}

}
  • Related