关于Java网络编程的问题,导入的包我已经省略
这是服务器的程序publicclassMyServer{publicstaticvoidmain(String[]args)throwsIOException{Syste...
这是服务器的程序
public class MyServer {
public static void main(String[] args)throws IOException {
System.out.println("我是服务器");
ServerSocket server=new ServerSocket(5678);
Socket client=server.accept();
BufferedReader in=new BufferedReader(new InputStreamReader(client.getInputStream()));
PrintWriter out=new PrintWriter(client.getOutputStream());
while(true){
String str=in.readLine();
System.out.println(str);
out.println("has receive.....");
out.flush();
if(str.equals("end"))
break;}
client.close();}}
这是客户端的程序
public class Client {
static Socket server;
public static void main(String[] args) throws Exception{
System.out.println("我是客户端");
server=new Socket(InetAddress.getLocalHost(),5678);
BufferedReader in=new BufferedReader(new InputStreamReader(server.getInputStream()));
PrintWriter out=new PrintWriter(server.getOutputStream());
BufferedReader wt=new BufferedReader(new InputStreamReader(System.in));
while(true){
String str=wt.readLine();
out.println(str);
out.flush();
if(str.equals("end")){
break;
}
System.out.println(in.readLine()) ; }
server.close();
}}
如果我想在控制台上实现服务器与客户端互相发送交流信息,
那么我怎么修改以上的程序,请求高手指教。我苦苦思索了好久了 展开
public class MyServer {
public static void main(String[] args)throws IOException {
System.out.println("我是服务器");
ServerSocket server=new ServerSocket(5678);
Socket client=server.accept();
BufferedReader in=new BufferedReader(new InputStreamReader(client.getInputStream()));
PrintWriter out=new PrintWriter(client.getOutputStream());
while(true){
String str=in.readLine();
System.out.println(str);
out.println("has receive.....");
out.flush();
if(str.equals("end"))
break;}
client.close();}}
这是客户端的程序
public class Client {
static Socket server;
public static void main(String[] args) throws Exception{
System.out.println("我是客户端");
server=new Socket(InetAddress.getLocalHost(),5678);
BufferedReader in=new BufferedReader(new InputStreamReader(server.getInputStream()));
PrintWriter out=new PrintWriter(server.getOutputStream());
BufferedReader wt=new BufferedReader(new InputStreamReader(System.in));
while(true){
String str=wt.readLine();
out.println(str);
out.flush();
if(str.equals("end")){
break;
}
System.out.println(in.readLine()) ; }
server.close();
}}
如果我想在控制台上实现服务器与客户端互相发送交流信息,
那么我怎么修改以上的程序,请求高手指教。我苦苦思索了好久了 展开
1个回答
推荐于2016-03-20
展开全部
我先运行看看,我也用过printwriter,不过代码找不到了
后来用的发送对象
我去,你这个都没有一个发送,都是发送一个接收到的消息,都没有发送,怎么会接收到消息,一直阻塞在读取那里
//改一点点就会有效果了
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
public class Client {
static Socket server;
public static void main(String[] args) throws Exception{
System.out.println("我是客户端");
server=new Socket("127.0.0.1",5678);
BufferedReader in=new BufferedReader(new InputStreamReader(server.getInputStream()));
PrintWriter out=new PrintWriter(server.getOutputStream());
BufferedReader wt=new BufferedReader(new InputStreamReader(System.in));
while(true){
String str="我是客户端";
System.out.println("阻塞");
out.println(str);
out.flush();
if(str.equals("end")){
break;
}
System.out.println(in.readLine()) ; }
server.close();
}}
我还写了图形化的聊天室,可以私聊,群聊,你需要也可以给你
更多追问追答
追问
明白了,谢谢
麻烦你把你写的图形化聊天室都发给我看看吧,谢谢了
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询