没见过的错误。。。 exceptionthreadsocket 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 貌似ServerSocket和Socket写在一个程序里不太好吧,写两个程序一个Client、一个Serversocket = serverSocket.accept();应该在serverSocket = new ServerSocket(4331);之前执行啊,提前监听嘛。 我是写了两个啊。。服务器端:package Server;import java.io.*;import java.net.*;import java.util.*;public class Server { public static void main(String[] args) { // TODO Auto-generated method stub LinkedList<Socket> linkedList= new LinkedList<Socket>(); ServerSocket serverSocket = null; Socket socket = null; while(true){ try { serverSocket = new ServerSocket(4331); } catch (IOException e) { // TODO Auto-generated catch block System.out.println("正在监听"); } try { System.out.println("等待客户呼叫"); socket = serverSocket.accept(); linkedList.addLast(socket); System.out.println("客户的地址:"+socket.getInetAddress()); } catch (IOException e) { // TODO Auto-generated catch block System.out.println("正在等待客户"); } if(socket != null){ new ServerThread(socket,linkedList).start(); } } }}package Server;import java.net.*;import java.io.*;import java.util.*;import sun.org.mozilla.javascript.internal.ObjToIntMap.Iterator;public class ServerThread extends Thread{ LinkedList<Socket> linkedlist = new LinkedList<Socket>(); Socket socket; DataOutputStream out = null; DataInputStream in = null; String s = null; public ServerThread(Socket t,LinkedList linkedList) { // TODO Auto-generated constructor stub this.linkedlist = linkedList; socket = t; try{ in = new DataInputStream(socket.getInputStream()); } catch(IOException e){} } public void run(){ while(true){ try { s = in.readUTF(); System.out.println(s); System.out.println(linkedlist.size()); WriteUTF(); } catch (IOException e) { // TODO Auto-generated catch block System.out.println("用户离开"); linkedlist.removeFirstOccurrence(socket); break; } } } public void WriteUTF(){ java.util.Iterator<Socket> it = linkedlist.iterator(); while(it.hasNext()){ try { Socket socket1 = it.next(); System.out.println(socket1); out = new DataOutputStream(socket1.getOutputStream()); out.writeUTF(s); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }}client端package Client;import java.net.*;import java.awt.*;import java.io.*;import java.awt.event.*;import javax.swing.*;public class WindowsClient extends JFrame implements Runnable,ActionListener{ private static final long serialVersionUID = 1L; JButton connection,send; JTextField inputText; JTextArea showResult; Socket socket = null; DataInputStream in = null; DataOutputStream out = null; Thread thread; public WindowsClient(){ socket = new Socket(); connection = new JButton("连接服务器"); send = new JButton("发送"); send.setEnabled(false); inputText = new JTextField(15); showResult = new JTextArea(); add(connection,BorderLayout.NORTH); JPanel pSouth = new JPanel(); pSouth.add(new JLabel("输入内容")); pSouth.add(inputText); pSouth.add(send); add(pSouth,BorderLayout.SOUTH); add(new JScrollPane(showResult),BorderLayout.CENTER); connection.addActionListener(this); send.addActionListener(this); thread = new Thread(this); setBounds(10,30,460,400); setVisible(true); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } public void run() { // TODO Auto-generated method stub String s = null; double result = 0; while(true){ try { s = in.readUTF(); showResult.append("\n"+s); } catch (IOException e) { // TODO Auto-generated catch block showResult.setText("已与服务器断开连接"+e); socket = new Socket(); break; } } } public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub if(e.getSource() == connection){ try { InetAddress address = InetAddress.getByName("127.0.0.1");; InetSocketAddress socketAddress = new InetSocketAddress(address,4331); socket.connect(socketAddress); in = new DataInputStream(socket.getInputStream()); out = new DataOutputStream(socket.getOutputStream()); send.setEnabled(true); if(!thread.isAlive()){ thread.start(); } } catch (Exception e1) { // TODO Auto-generated catch block e1.printStackTrace(); socket = new Socket(); } } if(e.getSource() == send){ try { String s = inputText.getText().trim(); out.writeUTF(s); } catch (IOException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } } }} 而且不应该先new一个serverSocket,才能执行之后的serverSocket.accept()啊怎么能放在new之前呢 首先我没有出现你说的问题,其次你客户端写的socket有问题,无法连接到server上,还有就是你的窗口发送按钮一直是灰色的,无法进行数据发送 我自己修改了一下子没问题啊,也没有改动太多,就是将你send.setEnabled(true);里面置为了true,以前没接触过awt 我自己修改了一下子没问题啊,也没有改动太多,就是将你send.setEnabled(true);里面置为了true,以前没接触过awt好的~谢谢哦~因为最近在搞Oracle所以没太看到~ 魔数不正确。你改过CLASS文件了么? .class文件必须以0xCAFEBABE开头,不然就直接报错了。你的编译工具是特别的么? 为何我的代码有连接到来的时候,CPU会达到100%占用 在jdbc中调用oracle的存储过程的out参数的问题,急 applet中的监听器 如何将IP地址转为整型 jdbc的驱动程序配置 请教各位,如何使用object类的equals方法~~?? 问一个菜鸟问题:) ▲高分求解▲:Unicode ——>> GB2312 java中怎么传递变量的值? 哪里可以下载到JDK的源代码? dom树如何解析 急!急!急!急!急!急!jxl导出Excel问题,NullPointerException
socket = serverSocket.accept();应该在serverSocket = new ServerSocket(4331);之前执行啊,提前监听嘛。
服务器端:
package Server;
import java.io.*;
import java.net.*;
import java.util.*;public class Server {
public static void main(String[] args) {
// TODO Auto-generated method stub
LinkedList<Socket> linkedList= new LinkedList<Socket>();
ServerSocket serverSocket = null;
Socket socket = null;
while(true){
try {
serverSocket = new ServerSocket(4331);
} catch (IOException e) {
// TODO Auto-generated catch block
System.out.println("正在监听");
}
try {
System.out.println("等待客户呼叫");
socket = serverSocket.accept();
linkedList.addLast(socket);
System.out.println("客户的地址:"+socket.getInetAddress());
} catch (IOException e) {
// TODO Auto-generated catch block
System.out.println("正在等待客户");
}
if(socket != null){
new ServerThread(socket,linkedList).start();
}
}
}}
package Server;
import java.net.*;
import java.io.*;
import java.util.*;import sun.org.mozilla.javascript.internal.ObjToIntMap.Iterator;public class ServerThread extends Thread{
LinkedList<Socket> linkedlist = new LinkedList<Socket>();
Socket socket;
DataOutputStream out = null;
DataInputStream in = null;
String s = null;
public ServerThread(Socket t,LinkedList linkedList) {
// TODO Auto-generated constructor stub
this.linkedlist = linkedList;
socket = t;
try{
in = new DataInputStream(socket.getInputStream());
}
catch(IOException e){}
}
public void run(){
while(true){
try {
s = in.readUTF();
System.out.println(s);
System.out.println(linkedlist.size());
WriteUTF();
} catch (IOException e) {
// TODO Auto-generated catch block
System.out.println("用户离开");
linkedlist.removeFirstOccurrence(socket);
break;
}
}
}
public void WriteUTF(){
java.util.Iterator<Socket> it = linkedlist.iterator();
while(it.hasNext()){
try {
Socket socket1 = it.next();
System.out.println(socket1);
out = new DataOutputStream(socket1.getOutputStream());
out.writeUTF(s);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
client端package Client;
import java.net.*;
import java.awt.*;
import java.io.*;
import java.awt.event.*;
import javax.swing.*;public class WindowsClient extends JFrame implements Runnable,ActionListener{ private static final long serialVersionUID = 1L;
JButton connection,send;
JTextField inputText;
JTextArea showResult;
Socket socket = null;
DataInputStream in = null;
DataOutputStream out = null;
Thread thread;
public WindowsClient(){
socket = new Socket();
connection = new JButton("连接服务器");
send = new JButton("发送");
send.setEnabled(false);
inputText = new JTextField(15);
showResult = new JTextArea();
add(connection,BorderLayout.NORTH);
JPanel pSouth = new JPanel();
pSouth.add(new JLabel("输入内容"));
pSouth.add(inputText);
pSouth.add(send);
add(pSouth,BorderLayout.SOUTH);
add(new JScrollPane(showResult),BorderLayout.CENTER);
connection.addActionListener(this);
send.addActionListener(this);
thread = new Thread(this);
setBounds(10,30,460,400);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void run() {
// TODO Auto-generated method stub
String s = null;
double result = 0;
while(true){
try {
s = in.readUTF();
showResult.append("\n"+s);
} catch (IOException e) {
// TODO Auto-generated catch block
showResult.setText("已与服务器断开连接"+e);
socket = new Socket();
break;
}
}
} public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if(e.getSource() == connection){
try {
InetAddress address = InetAddress.getByName("127.0.0.1");;
InetSocketAddress socketAddress = new InetSocketAddress(address,4331);
socket.connect(socketAddress);
in = new DataInputStream(socket.getInputStream());
out = new DataOutputStream(socket.getOutputStream());
send.setEnabled(true);
if(!thread.isAlive()){
thread.start();
}
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
socket = new Socket();
}
}
if(e.getSource() == send){
try {
String s = inputText.getText().trim();
out.writeUTF(s);
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}}
好的~谢谢哦~因为最近在搞Oracle所以没太看到~
你改过CLASS文件了么? .class文件必须以0xCAFEBABE开头,不然就直接报错了。
你的编译工具是特别的么?