Java报错,怎么回事求大神解决
服务器端importjava.io.IOException;importjava.io.PrintStream;importjava.net.ServerSocket;i...
服务器端
import java.io.IOException;
import java.io.PrintStream;
import java.net.ServerSocket;
import java.net.Socket;
public class Server {
/**
* @param args
*/
public static void main(String[] args) throws IOException
{
ServerSocket ss=new ServerSocket(32445);
while(true)
{
Socket s=ss.accept();
PrintStream ps=new PrintStream(s.getOutputStream());
ps.println("圣诞快乐");
ps.close();
s.close();
}
// TODO Auto-generated method stub
}
}
客户端
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.*;
public class Cilent {
/**
* @param args
*/
public static void main(String[] args) throws IOException
{
Socket socket=new Socket("127.0.0.1",32445);
BufferedReader br=new BufferedReader(new InputStreamReader(socket.getInputStream()));
String line=br.readLine();
System.out.print("来自服务器的数据" + line);
br.close();
socket.close();
// TODO Auto-generated method stub
}
}
报错信息
Exception in thread "main" java.net.ConnectException: Connection refused: connect
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:333)
at java.net.PlainSocketImpl.connectToAddress(PlainSocketImpl.java:195)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:182)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:366)
at java.net.Socket.connect(Socket.java:519)
at java.net.Socket.connect(Socket.java:469)
at java.net.Socket.<init>(Socket.java:366)
at java.net.Socket.<init>(Socket.java:180)
at Cilent.main(Cilent.java:12) 展开
import java.io.IOException;
import java.io.PrintStream;
import java.net.ServerSocket;
import java.net.Socket;
public class Server {
/**
* @param args
*/
public static void main(String[] args) throws IOException
{
ServerSocket ss=new ServerSocket(32445);
while(true)
{
Socket s=ss.accept();
PrintStream ps=new PrintStream(s.getOutputStream());
ps.println("圣诞快乐");
ps.close();
s.close();
}
// TODO Auto-generated method stub
}
}
客户端
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.*;
public class Cilent {
/**
* @param args
*/
public static void main(String[] args) throws IOException
{
Socket socket=new Socket("127.0.0.1",32445);
BufferedReader br=new BufferedReader(new InputStreamReader(socket.getInputStream()));
String line=br.readLine();
System.out.print("来自服务器的数据" + line);
br.close();
socket.close();
// TODO Auto-generated method stub
}
}
报错信息
Exception in thread "main" java.net.ConnectException: Connection refused: connect
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:333)
at java.net.PlainSocketImpl.connectToAddress(PlainSocketImpl.java:195)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:182)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:366)
at java.net.Socket.connect(Socket.java:519)
at java.net.Socket.connect(Socket.java:469)
at java.net.Socket.<init>(Socket.java:366)
at java.net.Socket.<init>(Socket.java:180)
at Cilent.main(Cilent.java:12) 展开
4个回答
展开全部
连接不上呗
你看看 Server有没有启动
你看看 Server有没有启动
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
先运行Server类,再运行Cilent
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
数据连接失败,你查看一下你本地的端口是否是可以用的
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询