我是在eclipse里面建立项目的,利用socket在服务器端和客户端之间传送
但是先启动客户端,再启动服务器端时,老是报端口被占用的异常,不知道怎么解决,问问各位!代码如下
服务器端import java.net.*;
import java.io.*;public class ServerApp
{
public static void main(String args[])
{
try
{
boolean flag=true;
Socket clientSocket=null;
String inputLine;
int c;
ServerSocket sSocket=new ServerSocket(8018);
System.out.println("Server listen on:"+sSocket.getLocalPort()); while(flag)
{
clientSocket=sSocket.accept();
DataInputStream is= new DataInputStream(new BufferedInputStream(clientSocket.getInputStream()));
OutputStream os=clientSocket.getOutputStream(); while((inputLine=is.readLine())!=null)
{
//当客户端输入stop的时候服务器程序运行终止!
if(inputLine.equals("stop"))
{
flag=false;
break;
}
else
{
System.out.println(inputLine); while((c=System.in.read())!=-1)
{
os.write((byte)c);
if(c=='\n')
{
os.flush(); //将信息发送到客户端
break;
}
}
}
}
is.close();
os.close();
clientSocket.close(); }
sSocket.close();
}
catch(Exception e)
{
System.out.println("Exception :"+ e.getMessage());
}
}
}客户端import java.io.*;
import java.net.Socket;public class ClientApp
{
public static void main(String args[])
{
try
{
//创建通讯并且和主机Rock连接
Socket cSocket=new Socket("192.168.1.15",8018);
//打开这个Socket的输入/输出流
OutputStream os=cSocket.getOutputStream();
DataInputStream is=new DataInputStream(cSocket.getInputStream()); int c;
boolean flag=true; String responseline; while(flag)
{
//从标准输入输出接受字符并且写如系统
while((c=System.in.read())!=-1)
{
os.write((byte)c);
if(c=='\n')
{
os.flush();
//将程序阻塞,直到回答信息被收到后将他们在标准输出上显示出来
responseline=is.readLine();
System.out.println("Message is:"+responseline);
}
}
}
os.close();
is.close();
cSocket.close(); }
catch(Exception e)
{
System.out.println("Exception :"+ e.getMessage());
}
}
}
但是先启动客户端,再启动服务器端时,老是报端口被占用的异常,不知道怎么解决,问问各位!代码如下
服务器端import java.net.*;
import java.io.*;public class ServerApp
{
public static void main(String args[])
{
try
{
boolean flag=true;
Socket clientSocket=null;
String inputLine;
int c;
ServerSocket sSocket=new ServerSocket(8018);
System.out.println("Server listen on:"+sSocket.getLocalPort()); while(flag)
{
clientSocket=sSocket.accept();
DataInputStream is= new DataInputStream(new BufferedInputStream(clientSocket.getInputStream()));
OutputStream os=clientSocket.getOutputStream(); while((inputLine=is.readLine())!=null)
{
//当客户端输入stop的时候服务器程序运行终止!
if(inputLine.equals("stop"))
{
flag=false;
break;
}
else
{
System.out.println(inputLine); while((c=System.in.read())!=-1)
{
os.write((byte)c);
if(c=='\n')
{
os.flush(); //将信息发送到客户端
break;
}
}
}
}
is.close();
os.close();
clientSocket.close(); }
sSocket.close();
}
catch(Exception e)
{
System.out.println("Exception :"+ e.getMessage());
}
}
}客户端import java.io.*;
import java.net.Socket;public class ClientApp
{
public static void main(String args[])
{
try
{
//创建通讯并且和主机Rock连接
Socket cSocket=new Socket("192.168.1.15",8018);
//打开这个Socket的输入/输出流
OutputStream os=cSocket.getOutputStream();
DataInputStream is=new DataInputStream(cSocket.getInputStream()); int c;
boolean flag=true; String responseline; while(flag)
{
//从标准输入输出接受字符并且写如系统
while((c=System.in.read())!=-1)
{
os.write((byte)c);
if(c=='\n')
{
os.flush();
//将程序阻塞,直到回答信息被收到后将他们在标准输出上显示出来
responseline=is.readLine();
System.out.println("Message is:"+responseline);
}
}
}
os.close();
is.close();
cSocket.close(); }
catch(Exception e)
{
System.out.println("Exception :"+ e.getMessage());
}
}
}
{
System.out.println("Exception :"+ e.getMessage());
}
这一句打印出: Exception :Address already in use: JVM_Bind
你用命令行启动的试一试。