java socket 的问题 服务器和客户端输入没反应,在对方的命令行窗口显示不出来 20
importjava.io.*;importjava.net.*;publicclassServer{publicstaticvoidmain(String[]args)...
import java.io.*;
import java.net.*;
public class Server {
public static void main(String[] args)throws Exception{
boolean f=true;
while(f){
try{
ServerSocket server=new ServerSocket(1234);
System.out.println("服务器在端口1234监听…………");
Socket socket=server.accept();
System.out.println("开始通信…………");
OutputStream out=socket.getOutputStream();
PrintWriter pw=new PrintWriter(out);
BufferedReader reader=new BufferedReader(new InputStreamReader(System.in));
pw.write("服务器:"+reader.readLine());
InputStreamReader in=new InputStreamReader(socket.getInputStream());
BufferedReader br=new BufferedReader(in);
String message=br.readLine();
System.out.println(message);
if("bye".equals(message))
f=false;
pw.close();
br.close();
socket.close();
server.close();
}catch(Exception e){
System.out.println(e);
}
}
}
}
import java.io.*;
import java.net.*;
public class Client {
public static void main(String[] args){
boolean f2=true;
while(f2){
try{
Socket socket=new Socket("127.0.0.1",1234);
InputStream in=socket.getInputStream();
BufferedReader br=new BufferedReader(new InputStreamReader(in));
String message=br.readLine();
System.out.println(message);
OutputStream out=socket.getOutputStream();
PrintWriter pw=new PrintWriter(out);
BufferedReader reader=new BufferedReader(new InputStreamReader(System.in));
pw.write("客户端:"+reader.readLine());
//服务器发来的信息
if("bye".equals(message))
f2=false;
pw.close();
br.close();
socket.close();
}catch(Exception e){
System.out.println(e);
}
}
}
} 展开
import java.net.*;
public class Server {
public static void main(String[] args)throws Exception{
boolean f=true;
while(f){
try{
ServerSocket server=new ServerSocket(1234);
System.out.println("服务器在端口1234监听…………");
Socket socket=server.accept();
System.out.println("开始通信…………");
OutputStream out=socket.getOutputStream();
PrintWriter pw=new PrintWriter(out);
BufferedReader reader=new BufferedReader(new InputStreamReader(System.in));
pw.write("服务器:"+reader.readLine());
InputStreamReader in=new InputStreamReader(socket.getInputStream());
BufferedReader br=new BufferedReader(in);
String message=br.readLine();
System.out.println(message);
if("bye".equals(message))
f=false;
pw.close();
br.close();
socket.close();
server.close();
}catch(Exception e){
System.out.println(e);
}
}
}
}
import java.io.*;
import java.net.*;
public class Client {
public static void main(String[] args){
boolean f2=true;
while(f2){
try{
Socket socket=new Socket("127.0.0.1",1234);
InputStream in=socket.getInputStream();
BufferedReader br=new BufferedReader(new InputStreamReader(in));
String message=br.readLine();
System.out.println(message);
OutputStream out=socket.getOutputStream();
PrintWriter pw=new PrintWriter(out);
BufferedReader reader=new BufferedReader(new InputStreamReader(System.in));
pw.write("客户端:"+reader.readLine());
//服务器发来的信息
if("bye".equals(message))
f2=false;
pw.close();
br.close();
socket.close();
}catch(Exception e){
System.out.println(e);
}
}
}
} 展开
展开全部
package com.v512.TestServer;
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.SocketException;
public class TestServer {
public static void main(String[] args) {
try {
ServerSocket s=new ServerSocket(8888);
Socket s1=s.accept();
OutputStream os=s1.getOutputStream();
DataOutputStream dos=new DataOutputStream(os);
InputStream is=s1.getInputStream();
DataInputStream dis=new DataInputStream(is);
InputStreamReader isr=new InputStreamReader(System.in);
BufferedReader br=new BufferedReader(isr);
String info;
while(true){
info=dis.readUTF();
System.out.println("对方说:"+info);
if(info.equals("bye"))
break;
info=br.readLine();
dos.writeUTF(info);
if(info.equals("bye"))
break;
}
dos.close();
dis.close();
s1.close();
}
catch (SocketException e) {
System.out.println("网络连接异常,程序退出!");
}
catch (IOException e) {
e.printStackTrace();
}
}
}
package com.v512.TestServer;
import java.net.Socket;
import java.net.SocketException;
import java.io.*;
public class TestClient {
public static void main(String[] args) {
try {
Socket s1 = new Socket("localhost",8888);
InputStream is=s1.getInputStream();
DataInputStream dis=new DataInputStream(is);
OutputStream os=s1.getOutputStream();
DataOutputStream dos=new DataOutputStream(os);
InputStreamReader isr=new InputStreamReader(System.in);
BufferedReader br=new BufferedReader(isr);
String info;
while(true){
info=br.readLine();
dos.writeUTF(info);
if(info.equals("bye"))
break;
info=dis.readUTF();
System.out.println("对方说:"+info);
if(info.equals("bye"))
break;
}
dis.close();
dos.close();
s1.close();
} catch (SocketException e) {
System.out.println("网络连接异常,程序退出!");
} catch (IOException e) {
e.printStackTrace();
}
}
}
尝试下我改的。。。。。
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.SocketException;
public class TestServer {
public static void main(String[] args) {
try {
ServerSocket s=new ServerSocket(8888);
Socket s1=s.accept();
OutputStream os=s1.getOutputStream();
DataOutputStream dos=new DataOutputStream(os);
InputStream is=s1.getInputStream();
DataInputStream dis=new DataInputStream(is);
InputStreamReader isr=new InputStreamReader(System.in);
BufferedReader br=new BufferedReader(isr);
String info;
while(true){
info=dis.readUTF();
System.out.println("对方说:"+info);
if(info.equals("bye"))
break;
info=br.readLine();
dos.writeUTF(info);
if(info.equals("bye"))
break;
}
dos.close();
dis.close();
s1.close();
}
catch (SocketException e) {
System.out.println("网络连接异常,程序退出!");
}
catch (IOException e) {
e.printStackTrace();
}
}
}
package com.v512.TestServer;
import java.net.Socket;
import java.net.SocketException;
import java.io.*;
public class TestClient {
public static void main(String[] args) {
try {
Socket s1 = new Socket("localhost",8888);
InputStream is=s1.getInputStream();
DataInputStream dis=new DataInputStream(is);
OutputStream os=s1.getOutputStream();
DataOutputStream dos=new DataOutputStream(os);
InputStreamReader isr=new InputStreamReader(System.in);
BufferedReader br=new BufferedReader(isr);
String info;
while(true){
info=br.readLine();
dos.writeUTF(info);
if(info.equals("bye"))
break;
info=dis.readUTF();
System.out.println("对方说:"+info);
if(info.equals("bye"))
break;
}
dis.close();
dos.close();
s1.close();
} catch (SocketException e) {
System.out.println("网络连接异常,程序退出!");
} catch (IOException e) {
e.printStackTrace();
}
}
}
尝试下我改的。。。。。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
先执行server,再执行Client,你用的127.0.0.1,别人的机器肯定是访问不了你的服务器的
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
2012-05-22
展开全部
把端口换成超过4000以后的试试
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询