Topic: Good Example. Plz, give me one point

  Print this page

1.Good Example. Plz, give me one point Copy to clipboard
Posted by: jam
Posted on: 2003-04-09 19:29

class A {
int i = 1;
int f() {
return i;
}
static char g() {
return 'A';
}
}

class B extends A {
int i = 2;
int f() {
return -1;
}
static char g() {
return 'B';
}
}

public class Test {
public static void main(String args[]) {
B b = new B();
System.out.println(b.i); // 2
System.out.println(b.f()); // -2
System.out.println(b.g()); // B
System.out.println(B.g()); // B

A a = (A) b;
System.out.println(a.i); // 1
System.out.println(a.f()); // -2
System.out.println(a.g()); // A
System.out.println(A.g()); // A
}
}

Hello, can give me one point, plz.

2.Re:Good Example. Plz, give me one point [Re: jam] Copy to clipboard
Posted by: why
Posted on: 2003-04-10 22:19

Question
請問是那門子的good example?
Question

3.Re:Good Example. Plz, give me one point [Re: why] Copy to clipboard
Posted by: floater
Posted on: 2003-04-11 00:43

why wrote:
Question
請問是那門子的good example?
Question

Hehe ..., I don't get it either, but can't reply in chinese.

4.Re:Good Example. Plz, give me one point [Re: floater] Copy to clipboard
Posted by: jam
Posted on: 2003-04-11 09:35

class A {
int i = 1;
int f() {
return i;
}
static char g() {
return 'A';
}
}

class B extends A {
int i = 2;
int f() {
return -1;
}
static char g() {
return 'B';
}
}

public class Test {
public static void main(String args[]) {
B b = new B();
System.out.println(b.i); // 2
System.out.println(b.f()); // -2
System.out.println(b.g()); // B
System.out.println(B.g()); // B

A a = (A) b;
System.out.println(a.i); // 1
// ****************
System.out.println(a.f()); // -2
// ****************
System.out.println(a.g()); // A
System.out.println(A.g()); // A
}
}

Sorry, I come from Taiwan, I can not post encoding by "GB2312" or "HZ"
The system will be using "Big5"

The exmaple is in "Java in a nutshell".
The chinese version is in "chapter 3" java object orient.
About subclass and inheritance, over write the method of super class.

I think it can be explain by "Polymorphism"

5.Re:Good Example. Plz, give me one point [Re: jam] Copy to clipboard
Posted by: floater
Posted on: 2003-04-11 14:20

Have you read the post in Java FAQ on how to get credit in this bbs?

6.Re:Good Example. Plz, give me one point [Re: jam] Copy to clipboard
Posted by: jiangns3000
Posted on: 2003-04-11 15:02

多作贡献多得分,加油干。

7.Re:Good Example. Plz, give me one point [Re: floater] Copy to clipboard
Posted by: jam
Posted on: 2003-04-11 16:07

Yes, I do. I have no idea to give point. It's relly difficult.
another question of ServerSocket and Socket ?

Client X only can use 8888 port, But Remote maybe can use different port.

Remote : A ServerSocket a = new ServerSocket(6666);
Remote : B ServerSocket b = new ServerSocket(7777);
Remote : C ServerSocket c = new ServerSocket(8888);
Remote : D ServerSocket d = new ServerSocket(9999);

Client X will send File A, B, C, D to Remote A, B, C, D

How can I do ?, If Remote's ServerSocket is all the same port on 8888,
It's easy. But They are different because of security on firewall.
I Had question before, But I cannot solve. So is not safe.
Help me, Plz.

8.Re:Good Example. Plz, give me one point [Re: jam] Copy to clipboard
Posted by: jiangns3000
Posted on: 2003-04-11 20:46

正常情况下,考虑的是Server方:四个线程,分别对应ServerSocket的6666,7777,8888,9999。然后Client方:四个线程,分别对Server方。
我想,问题的不同之处是:Client只能用一个IP及一个8888。此时按上述思路写C/S程序,运行时Server方无问题,但Client方只有一个线程正常通信,其余线程抛出:java.net.BindException: Address already in use: JVM_Bind
目前似乎重启JVM后IP方可重新bind。有何良方?

9.Re:Good Example. Plz, give me one point [Re: jam] Copy to clipboard
Posted by: jiangns3000
Posted on: 2003-04-11 20:50

难道只能用四个不同的JAVA程序做Client(因Server方已肯定无问题)而不能只用一个程序(多个线程)?

10.Re:Good Example. Plz, give me one point [Re: jam] Copy to clipboard
Posted by: jiangns3000
Posted on: 2003-04-11 22:33

即使只用一个程序(不用多线程)依次与server的四个6666,7777,8888,9999通信,还是同样问题,即只有第一个正常通信,完后其它抛出:java.net.BindException: Address already in use: JVM_Bind。

11.Re:Good Example. Plz, give me one point [Re: jam] Copy to clipboard
Posted by: jiangns3000
Posted on: 2003-04-11 23:10

问题已解决。原因是用完后忘记关闭socket。
建议jam:
Server方JAVA程序创建四个线程。
Client方JAVA程序(只用一个固定IP与一个固定8888port)只能一个接一个与Server通信。若用多线程则抛出上述Exception。
不知有没有更好的方案。

12.Re:Good Example. Plz, give me one point [Re: jam] Copy to clipboard
Posted by: floater
Posted on: 2003-04-12 07:27

Could we follow the idea of how web servers work?

13.Re:Good Example. Plz, give me one point [Re: floater] Copy to clipboard
Posted by: jam
Posted on: 2003-04-14 10:03

This new question is application service.
I think, I cannot use socket to solve it.
I need to find new API to do.
All the question is about port. if port are the same, there are no problem.
But the requirement is different. I still have no idea how to do.
Who can help me?

import java.io.*;
import java.net.*;
public class Server extends java.lang.Thread {
public void run() {
// one of the remote
int port = 5555; // maybe 6666, 7777, 8888
ServerSocket serverSocket = new ServerSocket(port);
while (true) {
try {
Socket socket = serverSocket.accept();
//socket.getInputStream();
//socket.getOutputStream();
}
catch (Exception e) {
System.out.println(e.toString());
}
}
}
}

import java.io.*;
import java.net.*;
public class Client {
public void send() {
// If
String[] hosts = {"192.168.0.11", "192.168.0.12", "192.168.0.13", "192.168.0.14"};
int[] ports = {5555, 6666, 7777, 8888};
for (int i=0; i<hosts.length; i++) {
Socket socket = new Socket(hosts[i], ports[i]);
//send different data to different remote.
//If the firewall just open 9911 port can send data
// and open 9922 port can receive data.
// but remote only open one port to send and receive
// with different port.
}
}
}

14.Re:Good Example. Plz, give me one point [Re: jam] Copy to clipboard
Posted by: jam
Posted on: 2003-04-14 10:12

Sorry, I forgot explain.
Client is using by another, maybe one hour, two hour to run according the information of database.

Remotes like many sub stores. They do not know when to receive data. So they are running all the time. To listen to Client (Provider).

15.Re:Good Example. Plz, give me one point [Re: jam] Copy to clipboard
Posted by: jam
Posted on: 2003-04-14 15:09

Sorry, I have meager knowledge. I think the sending and receiving socket port is the same. I make a big mistake, they are different. I know how to do.

// At Client
InetAddress local = InetAddress.getByName(null);
Socket socket = new Socket(remotehost, remoteport, local, localport);

Thaks all to help me.
And "floater" excellency can give alms to the poor one point. Plz

16.Re:Good Example. Plz, give me one point [Re: jam] Copy to clipboard
Posted by: jam
Posted on: 2003-04-15 13:54

Does the Remote A just only permit port 6000 to "send" and "receive", Remote B port 7000, Remote C port 8000, Remote port 9000 and Client permit port 9999 is correct? Can it work ?

Using
int localport = 9999;
//remote port 6000, 7000, 8000, 9000
InetAddress local = InetAddress.getByName(null);
Socket socket = new Socket(remotehost, remoteport, local, localport);


   Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent
Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1
客服电话 18559299278    客服信箱 714923@qq.com    客服QQ 714923