java大神请出来的_JAVA问题,请大神

建立个服务器与客户的聊天程序,但无法输入,以下为程序,求解答

package com.socket;

import java.io.BufferedReader;

import java.io.IOException;

import java.io.InputStream;

import java.io.InputStreamReader;

import java.io.OutputStream;

import java.io.PrintStream;

import java.net.ServerSocket;

import java.net.Socket;

public class TestSersocket {

public static void main(String[] args) throws Exception {

// TODO Auto-generated method stub

ServerSocket ss=new ServerSocket(7777);

System.out.println("服务器已启动");

Socket socket=ss.accept();

InputStream is=socket.getInputStream();

BufferedReader br=new BufferedReader(new InputStreamReader(is));

String st=null;

while((st=br.readLine())!=null){

System.out.println("client:"+st);

}

OutputStream os=socket.getOutputStream();

PrintStream ps=new PrintStream(os);

new readKeyboard(ps).start();

}

}

class readKeyboard extends Thread{

PrintStream ps;

@Override

public void run() {

BufferedReader br=new BufferedReader(new InputStreamReader(http://System.in));

String st=null;

try {

while((st=br.readLine())!=null){

ps.println(st);

}

} catch (IOException e) {

e.printStackTrace();

}

}

public readKeyboard(PrintStream ps){

this.ps=ps;

}

}

-----------------------------------------------------------------------

package com.socket;

import java.io.BufferedReader;

import java.io.IOException;

import java.io.InputStream;

import java.io.InputStreamReader;

import java.io.OutputStream;

import java.io.PrintStream;

import java.net.InetAddress;

import java.net.Socket;

import java.net.UnknownHostException;

public class TestClisocket {

public static void main(String[] args) throws Exception {

// TODO Auto-generated method stub

//InetAddress is=InetAddress.getLocalHost();

//System.out.println(is);

Socket sc=new Socket(InetAddress.getLocalHost(),7777);

System.out.println("服务器已连接");

OutputStream os=sc.getOutputStream();

PrintStream ps=new PrintStream(os);

new Thread(new Runnable(){

@Override

public void run() {

BufferedReader br=new BufferedReader(new InputStreamReader(http://System.in));

String st=null;

try {

while((st=br.readLine())!=null){

ps.print(st);

}

} catch (IOException e) {

e.printStackTrace();

}

}

}).start();

InputStream is=sc.getInputStream();

BufferedReader br=new BufferedReader(new InputStreamReader(is));

String st=null;

while((st=br.readLine())!=null){

System.out.println("server:"+st);

}

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值