很抱歉,第十行代码打印错误.应该是:bind(myIP,6501);
解决方案 »
- 看看这个
- 问下关于对象转型的问题,谢谢
- 【新手提问】请问怎么设置标题栏.我想把这个图片作登陆背景怎么实现呢?里面有图.请前辈帮忙.谢谢
- 谁帮我解决一下这个事件驱动的问题
- 棘手问题
- 求助:我想买一本有关JAVA标准类库的书,大家知道买哪本比较好?
- 求高手点拨:“我的程序中的错误原因”,一个关于集合的使用的技术问题...
- 大虾们,教教我这个新手,关于read()和readLine()给金!!!!!!!!!!!!!!!!!!!!!
- JDialog是不是分为模态和非模态呀?我怎么定义一个模态的jdialog呀?
- 赐教:工作流引擎怎样实现?!!
- 求JAVA IDE!!!
- 调查:你们在转正以后工资会涨多少?绝对给分,我有6000分的哦!
bind所在的包是:java.net.ScoketImpl 我在前面有import java.net.*;
import java.net.*;
public class Class1
{
public void Bind()
{
try{
InetAddress myIP=InetAddress.getLocalHost();
Socket sock=new Socket(myIP,5720);
bind(myIP,6501);
}
catch(UnknownHostException excpt)
{
System.err.println("Unknown host:"+excpt);
}
catch(IOException excpt)
{
System.err.println("I/O故障:"+excpt);
}
} /**
* Method bind.
* @param myIP
* @param i
*/
private void bind(InetAddress myIP, int i)
{
}}
要定义bind方法;然后才去调用呀
还有,想问一句,你学java多久?
为什么对象还没定义,就拿着它的方法来用,先打好基础要紧哟!
mysi.bind()
但是里面bind是声明为 protected abstract void bind(InetAddress host, int port) throws IOException
其中有abstract 所以你必须重新定义其方法的实现!有关java.net.SocketImpl的详细资料在这里:
http://kbs.cs.tu-berlin.de/~jutta/ht/apibook/javaf7.htm#31923
import java.io.*;public class Server
{
ServerSocket server;
DataOutputStream output;
Socket socket;
public Server (){
try{
// create a server on port 5000
server=new ServerSocket(5000);
// display interactive informaion
System.out.println("Server created.");
System.out.println("waiting for client to connect on...");
// waiting for client to connect on...
socket = server.accept();
// client connected
System.out.println("client connected.\nShutdown!");
output = new DataOutputStream(socket.getOutputStream());
output.writeUTF("Welcome to Server.Bye!");
output.close();
server.close();
}
catch(SocketException e){
System.out.println(e.toString());
e.printStackTrace();
System.exit(1);
}
catch(IOException e){
System.out.println(e.toString());
e.printStackTrace();
System.exit(1);
}
} public static void main(String args[]){
Server game=new Server();
}
}server 端的程序;import java.io.*;
import java.net.*;public class Client {
public static void main(String args[]) {
try{
if (args.length != 1){
System.out.println("USAGE: java Client servername");
return;
}
String connectto= args[0];
Socket connection;
// connect to server
if(connectto.equals("localhost")){
connection=new Socket(InetAddress.getLocalHost(),5000); }
else{
connection=new Socket(InetAddress.getByName(connectto),5000);
}
DataInputStream input=new DataInputStream(connection.getInputStream()); // read information from server
String info;
info = input.readUTF();
System.out.println(info);
connection.close();
}
catch(SecurityException e){
System.out.println("SecurityException when connecting Server!");
}
catch(IOException e){
System.out.println("IOException when connecting Server!");
}
}}
client端的程序;