我用NetBeans建立了一个jframe窗口!
添加了,一个按钮,一个文本,一个label
程序大概:
import java.io.*;
import java.net.*;
//类
Socket socket;
ServerSocket server;
DataInputStream in;
DataOutputStream out;
//接着是触发事件
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
int port;
port=Integer.valueOf(tf3.getText());
try {
server = new ServerSocket(port);
socket=server.accept();
in=new DataInputStream(socket.getInputStream());
out=new DataOutputStream(socket.getOutputStream());
l8.setText("连接状态:主机建立!");
} catch (IOException ex) {
l8.setText("连接状态:失败!");
}
}
但是不能建立,也不抛出错误,我很困惑,求解答,在线等,100分
添加了,一个按钮,一个文本,一个label
程序大概:
import java.io.*;
import java.net.*;
//类
Socket socket;
ServerSocket server;
DataInputStream in;
DataOutputStream out;
//接着是触发事件
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
int port;
port=Integer.valueOf(tf3.getText());
try {
server = new ServerSocket(port);
socket=server.accept();
in=new DataInputStream(socket.getInputStream());
out=new DataOutputStream(socket.getOutputStream());
l8.setText("连接状态:主机建立!");
} catch (IOException ex) {
l8.setText("连接状态:失败!");
}
}
但是不能建立,也不抛出错误,我很困惑,求解答,在线等,100分
不过建立主机的时候就已经开始出现问题了,客户端倒是没什么问题!
好像是socket=server。accpet()
那有问题,可不监听,有没办法,传递信息!
server = new ServerSocket(port);
socket=server.accept();
in=new DataInputStream(socket.getInputStream());
out=new DataOutputStream(socket.getOutputStream());
l8.setText("连接状态:主机建立!");
}
试下
server = new ServerSocket(port);
while(true){
socket=server.accept();
in=new DataInputStream(socket.getInputStream());
out=new DataOutputStream(socket.getOutputStream());
l8.setText("连接状态:主机建立!");
}
import java.net.*;
import java.io.*;
import java.util.logging.Level;
import java.util.logging.Logger;public class game extends javax.swing.JFrame {
Socket socket;
ServerSocket server;
DataInputStream in;
DataOutputStream out;
public game() {
initComponents();
}
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
int port;
port=Integer.valueOf(tf3.getText());
try {
server = new ServerSocket(port);
while(true) {
socket=server.accept();
in=new DataInputStream(socket.getInputStream());
out=new DataOutputStream(socket.getOutputStream());
l8.setText("连接状态:主机建立!");
}
} catch (IOException ex) {
l8.setText("连接状态:失败!");
}
}private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
int port=Integer.valueOf(tf5.getText());
String s=tf4.getText();
try {
InetAddress address = InetAddress.getByName(s);
//InetSocketAddress link=new InetSocketAddress(address,port);
try {
socket=new Socket(address,port);
in=new DataInputStream(socket.getInputStream());
out=new DataOutputStream(socket.getOutputStream());
l8.setText("连接状态:已连接");
} catch (IOException ex) {
l8.setText("连接状态:连接失败");
}
} catch (UnknownHostException ex) {
l8.setText("连接状态:找不到主机");
}
} private void formComponentShown(java.awt.event.ComponentEvent evt) {
// TODO add your handling code here:
InetAddress f;
try {
f = InetAddress.getLocalHost();
tf2.setText(f.getHostAddress());
tf1.setText(f.getHostName());
} catch (UnknownHostException ex) {
Logger.getLogger(game.class.getName()).log(Level.SEVERE, null, ex);
}
} private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
String s;
s=tz2.getText();
try {
out.writeUTF(s);
} catch (IOException ex) {
Logger.getLogger(game.class.getName()).log(Level.SEVERE, null, ex);
}
}private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
String s;
int a,b;
try {
s = in.readUTF();
tz1.setText(s);
} catch (IOException ex) {
t.append("对方还没有出招!");
}
} /**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new game().setVisible(true);
}
});
} }
在逻辑上我实在想不出我错在哪了!输入端口号,建立主机,监听,阻塞等待,然后从开一个同样的程序,输入地址,端口,连接!然后用输入输出流读写!
我吧socket=server。accept删了,就能够建立,可是删了,我还怎么传信息!
给你个例子public void actionPerformed(ActionEvent e) {
if (e.getActionCommand() == "新建") {
newFiel();
} else if (e.getActionCommand() == "打开") {
 ,白洁; openFile();
} else if (e.getActionCommand() == "保留") {
saveFiel();
 ,白洁;} else if (e.getActionCommand() == "退出") {
System.exit(0);
}
}
我想应该不是事件的问题,还是我建主机有问题!
窗口的接口不是接上了吗??extends javax.swing.JFrame
监视器是什么??求解答,谢谢!
import java.awt.event.*;
public class TestActionEvent1
{
public static void main(String[] args)
{
Frame f=new Frame("test");
f.setBounds(300,300,200,200);
Button b1=new Button("start");
Button b2=new Button("stop");
Monitor bh=new Monitor();//自定义的类
b1.addActionListener(bh);
b2.addActionListener(bh);
b2.setActionCommand("game over");
f.add(b1,BorderLayout.CENTER);
f.add(b2,BorderLayout.NORTH);
f.pack();//Frame.pack()这个方法的作用就是根据窗口里面的布局及组件的preferedSize来确定frame的最佳大小。
f.setVisible(true);
}
}
class Monitor implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
System.out.println("don't press me!!!!!!!!!!!!"+e.getActionCommand());
}
}
http://blog.hi.mop.com/GetEntry.do?id=7731367&owner=27610992
那个我刚刚不用gui试了一下,我发现即使单纯的写:
ublic class f2 {
public static void main(String arg[])
{
try {
ServerSocket server = new ServerSocket(8000);
Socket socket;
socket=server.accept();
DataInputStream in=new DataInputStream(socket.getInputStream());
DataOutputStream out=new DataOutputStream(socket.getOutputStream());
System.out.println("建立成功");
} catch (IOException ex) {
System.out.println("建立失败");
}
}
}这个也抛出了错误!我也不是第一次写这东西,这段时间也都在用,虽说我自己对自己的要求是一降再降,可现在我发现我好像连主机都建立不对!
有问题:运行结果:会显示连接失败!
抛出了IOException错误!
当客户端有个clientSocket访问服务器时,就会产生如下
while(true){
Socket clientSocket=socket.accept();
...开个线程去处理clientSocket,让socket继续保持监听.
}
这个clientSocket与客户端的主机是相关的,
然后要为这个clentSocket开一个线程去处理。
你代码的错误在你写clientSocket时写成同步了,Socket编程要写成异步。
在异步线程中,读写操(服务器端)作要在一个循环中进行,这样服务器端对应的socket才会不停的接收客户端的数据。