当前位置: 技术问答>java相关
中秋送礼!!高分相送啊!!
来源: 互联网 发布时间:2015-09-10
本文导语: 我的代码如下: import java.io.*; 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.pri...
我的代码如下:
import java.io.*;
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);
}
}
}
编译提示:Undefined name 'bind' (J0049)
不知道是什么原因及如何改.请帮帮小弟吧! :)
import java.io.*;
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);
}
}
}
编译提示:Undefined name 'bind' (J0049)
不知道是什么原因及如何改.请帮帮小弟吧! :)
|
bind 在java.net.SocketImpl包中,
但是里面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
但是里面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.net.*;
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端的程序;
import java.net.*;
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端的程序;
|
bind()方法在SocketImpl中被申明为protected,当然不能直接调用啦!
还有,想问一句,你学java多久?
为什么对象还没定义,就拿着它的方法来用,先打好基础要紧哟!
还有,想问一句,你学java多久?
为什么对象还没定义,就拿着它的方法来用,先打好基础要紧哟!
|
import java.io.*;
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方法;然后才去调用呀
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方法;然后才去调用呀
|
建议你查看一下SocktImpl下面的bind方法如何来用
|
你没有加入bind所在的包啊!
|
bind(myIp,6501);
没定义
没定义
|
没有定义bind方法,必须先定义一个bind方法,才能用
|
bind()没有定义
|
呵呵,楼主概念混淆了
|
试试Socket mysi = new SocketImpl()
mysi.bind()
mysi.bind()
|
up
您可能感兴趣的文章:
本站(WWW.)旨在分享和传播互联网科技相关的资讯和技术,将尽最大努力为读者提供更好的信息聚合和浏览方式。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。