- public class Client {
- public void run(){
- try {
- Socket socket=new Socket("127.0.0.1",9090);
-
- //创建对象输入流
- ObjectInputStream in=
- new ObjectInputStream(socket.getInputStream());
- ObjectOutputStream out=
- new ObjectOutputStream(socket.getOutputStream());
- System.out.println("请输入字符串:");
- Scanner sc =new Scanner(System.in);
- String s=sc.next();
- out.writeObject(s);
- out.flush();
- s=(String)in.readObject();
- System.out.println("服务器返回的字符串:");
- System.out.print(s);
- in.close();
- out.close();
- socket.close();
- } catch (IOException e) {
- e.printStackTrace();
- }catch(ClassNotFoundException e){
- e.printStackTrace();
- }
- }
- public static void main(String[] args) {
- Client client=new Client();
- client.run();
- }
- }
复制代码
代码如上 可以和服务器建立连接 但是无法传送数据,网上说是线程阻塞 但不明白为什么会阻塞 |