帮忙找一下有关问题所在
帮忙找一下问题所在.
客户端:
package src;
import java.net.*;
import java.io.*;
public class TCPClient {
public static void main(String[] args) throws Exception{
Socket s=new Socket( "localhost ",8080);
PrintWriter out=new PrintWriter(s.getOutputStream());
BufferedReader in=new BufferedReader(
new InputStreamReader(s.getInputStream()));
out.println(args[0]);
out.flush();
System.out.println(in.readLine());
System.out.println(in.readLine());
s.close();
}
}
服务器端:
import java.net.*;
import java.io.*;
public class TCPServer {
public static void main(String[] args) throws Exception{
ServerSocket ss=new ServerSocket(8080);
int id=0;
while(true){
Socket s=new Socket();
s=ss.accept();
id++;
new MyThread(id,s).start();
}
}
}
class MyThread extends Thread{
int id;
Socket s;
BufferedReader br;
PrintWriter pw;
public MyThread(int id,Socket s){
super();
this.id=id;
this.s=s;
}
public void run(){
try{
br=new BufferedReader(new InputStreamReader(s.getInputStream()));
pw=new PrintWriter(s.getOutputStream());
String str=br.readLine();
str=str.toUpperCase();
pw.println(str);
pw.println( "you are "+id+ " client ");
pw.flush();
s.close();
}catch(IOException ie){
ie.printStackTrace();
}
}
}
编译出现的异常:
Exception in thread "main " java.net.BindException: Address already in use: JVM_Bind
at java.net.PlainSocketImpl.socketBind(Native Method)
at java.net.PlainSocketImpl.bind(PlainSocketImpl.java:359)
at java.net.ServerSocket.bind(ServerSocket.java:319)
at java.net.ServerSocket. <init> (ServerSocket.java:185)
at java.net.ServerSocket. <init> (ServerSocket.java:97)
at src.TCPServer.main(TCPServer.java:7)
希望帮忙找一下问题,谢谢.
------解决方案--------------------
Socket s=new Socket( "localhost ",8080);
端口被占用,换个端口
------解决方案--------------------
JVM_Bind, bind是什么意思?
一般就是端口被占用
客户端:
package src;
import java.net.*;
import java.io.*;
public class TCPClient {
public static void main(String[] args) throws Exception{
Socket s=new Socket( "localhost ",8080);
PrintWriter out=new PrintWriter(s.getOutputStream());
BufferedReader in=new BufferedReader(
new InputStreamReader(s.getInputStream()));
out.println(args[0]);
out.flush();
System.out.println(in.readLine());
System.out.println(in.readLine());
s.close();
}
}
服务器端:
import java.net.*;
import java.io.*;
public class TCPServer {
public static void main(String[] args) throws Exception{
ServerSocket ss=new ServerSocket(8080);
int id=0;
while(true){
Socket s=new Socket();
s=ss.accept();
id++;
new MyThread(id,s).start();
}
}
}
class MyThread extends Thread{
int id;
Socket s;
BufferedReader br;
PrintWriter pw;
public MyThread(int id,Socket s){
super();
this.id=id;
this.s=s;
}
public void run(){
try{
br=new BufferedReader(new InputStreamReader(s.getInputStream()));
pw=new PrintWriter(s.getOutputStream());
String str=br.readLine();
str=str.toUpperCase();
pw.println(str);
pw.println( "you are "+id+ " client ");
pw.flush();
s.close();
}catch(IOException ie){
ie.printStackTrace();
}
}
}
编译出现的异常:
Exception in thread "main " java.net.BindException: Address already in use: JVM_Bind
at java.net.PlainSocketImpl.socketBind(Native Method)
at java.net.PlainSocketImpl.bind(PlainSocketImpl.java:359)
at java.net.ServerSocket.bind(ServerSocket.java:319)
at java.net.ServerSocket. <init> (ServerSocket.java:185)
at java.net.ServerSocket. <init> (ServerSocket.java:97)
at src.TCPServer.main(TCPServer.java:7)
希望帮忙找一下问题,谢谢.
------解决方案--------------------
Socket s=new Socket( "localhost ",8080);
端口被占用,换个端口
------解决方案--------------------
JVM_Bind, bind是什么意思?
一般就是端口被占用