为什么我电脑的IP地址总是在变?通常是127.0.0.1……??是物理地址?还是……??急急急
可以参看我的一个Java多客户程序:
[code=Java][/code]
//服务器端
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class MultipleWindowsDemo extends JFrame
implements ActionListener {
private JTextArea jta;
private JButton jbtShowHistogram = new JButton("Show Histogram");
private Histogram histogram = new Histogram();
// Create a new frame to hold the histogram panel
private JFrame histogramFrame = new JFrame();
public MultipleWindowsDemo() {
// Store text area in a scroll pane
JScrollPane scrollPane = new JScrollPane(jta = new JTextArea());
scrollPane.setPreferredSize(new Dimension(300, 200));
jta.setWrapStyleWord(true);
jta.setLineWrap(true);
// Place scroll pane and button in the frame
getContentPane().add(scrollPane, BorderLayout.CENTER);
getContentPane().add(jbtShowHistogram, BorderLayout.SOUTH);
// Register listener
jbtShowHistogram.addActionListener(this);
// Create a new frame to hold the histogram panel
histogramFrame.getContentPane().add(histogram);
histogramFrame.pack();
histogramFrame.setTitle("Histogram");
}
/** Handle the button action */
public void actionPerformed(ActionEvent e) {
// Count the letters in the text area
int[] count = countLetters();
// Set the letter count to histogram for display
histogram.showHistogram(count);
// Show the frame
histogramFrame.setVisible(true);
}
/** Count the letters in the text area */
private int[] countLetters() {
// Count for 26 letters
int[] count = new int[26];
// Get contents from the text area
String text = jta.getText();
// Count occurrence of each letter (case insensitive)
for (int i = 0; i < text.length(); i++) {
char character = text.charAt(i);
if ((character >= 'A') && (character <= 'Z')) {
count[(int)character - 65]++; // The ASCII for 'A' is 65
}
else if ((character >= 'a') && (character <= 'z')) {
count[(int)character - 97]++; // The ASCII for 'a' is 97
}
}
return count; // Return the count array
}
public static void main(String[] args) {
MultipleWindowsDemo frame = new MultipleWindowsDemo();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setTitle("MultipleWindowsDemo");
frame.pack();
frame.setVisible(true);
}
}
//客户端
import java.io.*;
import java.net.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Client extends JFrame implements ActionListener {
// Text field for receiving radius
private JTextField jtf = new JTextField();
// Text area to display contents
private JTextArea jta = new JTextArea();
// IO streams
private DataOutputStream outputToServer;
private DataInputStream inputFromServer;
public static void main(String[] args) {
new Client();
}
public Client() {
// Panel p to hold the label and text field
JPanel p = new JPanel();
p.setLayout(new BorderLayout());
p.add(new JLabel("Enter radius"), BorderLayout.WEST);
p.add(jtf, BorderLayout.CENTER);
jtf.setHorizontalAlignment(JTextField.RIGHT);
getContentPane().setLayout(new BorderLayout());
getContentPane().add(p, BorderLayout.NORTH);
getContentPane().add(new JScrollPane(jta), BorderLayout.CENTER);
jtf.addActionListener(this); // Register listener
setTitle("Client");
setSize(500, 300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true); // It is necessary to show the frame here!
try {
// Create a socket to connect to the server
Socket socket = new Socket("localhost", 8000);
// Socket socket = new Socket("130.254.204.36", 8000);
// Socket socket = new Socket("drake.Armstrong.edu", 8000);
// Create an input stream to receive data from the server
inputFromServer = new DataInputStream(
socket.getInputStream());
// Create an output stream to send data to the server
outputToServer =
new DataOutputStream(socket.getOutputStream());
}
catch (IOException ex) {
jta.append(ex.toString() + '\n');
}
}
public void actionPerformed(ActionEvent e) {
String actionCommand = e.getActionCommand();
if (e.getSource() instanceof JTextField) {
try {
// Get the radius from the text field
double radius = Double.parseDouble(jtf.getText().trim());
// Send the radius to the server
outputToServer.writeDouble(radius);
outputToServer.flush();
// Get area from the server
double area = inputFromServer.readDouble();
// Display to the text area
jta.append("Radius is " + radius + "\n");
jta.append("Area received from the server is "
+ area + '\n');
}
catch (IOException ex) {
System.err.println(ex);
}
}
}
}
[解决办法]
127.0.0.1 永远指向本地
其他的地址可以映射,在
C:\WINDOWS\system32\drivers\etc\hosts
具体你要问什么再描述一次
[解决办法]
127.0.0.0是本机IP。永远也是。其它的内网IP可以自己设定为不变。但是外网IP一般都是动态的,如果你要静态的外网IP需要与你的网络运营商联系
[解决办法]