聊天小程序客户端不能向服务器端发信息如何解决? 400 报错
小程序客户端:
import java.awt.*;import java.awt.event.*;
import javax.swing.*;
import java.io.*;
import java.net.*;
public class Client {
public static void main(String [] agrs){
new MyWindow();
}
}
class MyWindow extends JFrame implements ActionListener,WindowListener{
public static JTextField text1;
public static JTextField text2;
public static JTextArea text3;
public static Socket [] client=new Socket[4];
public static JRadioButton[]jRadio=new JRadioButton[4];
public static ClientThread []thread=new ClientThread[4];
MyWindow(){
super("客户程序");
for(int i=0;i<4;i++){
jRadio[i]=new JRadioButton("对话者"+i);
thread[i]=null;
}
jRadio[0].setSelected(true);
text1=new JTextField();
text2=new JTextField();
text3=new JTextArea();
this.setLayout(null);
JScrollPane jsp=new JScrollPane(text3);
jsp.setBounds(10,10,400,400);
text1.setBounds(10,420,400,30);
text2.setBounds(10,460,400,30);
JPanel pan=new JPanel();
ButtonGroup g=new ButtonGroup();
for(int i=0;i<4;i++){
g.add(jRadio[i]);
pan.add(jRadio[i]);
}
pan.setBounds(10,500,400,30);
this.add(jsp);this.add(text1);this.add(text2);this.add(pan);
this.setSize(420,570);
this.setLocation(300,200);
this.setResizable(false);
this.setVisible(true);
text1.addActionListener(this);
text2.addActionListener(this);
this.addWindowListener(this);
}
@SuppressWarnings("deprecation")
public void windowClosing(WindowEvent e){
for(int i=0;i<4;i++){
if(client[i]!=null){
try{
this.thread[i].socket.close();
client[i].close();
}catch(IOException e1){}
}
}
}
public void windowActivated(WindowEvent e){}
public void windowClosed(WindowEvent e){
for(int i=0;i<4;i++){
if(client[i]!=null){
try{
this.thread[i].socket.close();
client[i].close();
}catch(IOException e1){}
}
}
}
public void windowDeactivated(WindowEvent e){}
public void windowDeiconified(WindowEvent e){}
public void windowIconified(WindowEvent e){}
public void windowOpened(WindowEvent e){}
public void actionPerformed(ActionEvent e){
if(e.getSource()==text1){
for(int i=0;i<4;i++){
if(client[i]==null){
try{
client[i]=new Socket(text1.getText(),4441);
}catch(IOException g){
text3.append("链接出错!!!"+"\n");
}
thread[i]=new ClientThread(client[i],i);
thread[i].start();
jRadio[i].setText(text1.getText());
break;
}
}
}else if(e.getSource()==text2){
for(int i=0;i<4;i++){
if(jRadio[i].isSelected()==true){
try{
thread[i].out.writeUTF(text2.getText());
text3.append(text2.getText()+"\n");
if(text2.getText().equals("结束")){
client[i].close();
client[i]=null;
thread[i].socket.close();
jRadio[i].setText("对话者"+i);
}
text2.setText("");
}catch(IOException g2){
text3.append("读写出错!!!"+"\n");
}
}
}
}
}
}
class ClientThread extends Thread{
int i;
String s;
Socket socket;
public DataInputStream in;
public DataOutputStream out;
ClientThread(Socket s1,int j){
i=j;
socket=s1;
try{
in=new DataInputStream(socket.getInputStream());
out=new DataOutputStream(socket.getOutputStream());
}catch(IOException g){
MyWindow.text1.setText("读写出错!!!");
}
}
public void run(){
while(true){
try{
s=in.readUTF();
if(s.equals("结束")){
socket.close();
MyWindow.client[i].close();
MyWindow.client[i]=null;
MyWindow.jRadio[i].setText("对话者"+i);
break;
}
MyWindow.text3.append(s+"\n");
}catch(IOException g1){}
}
}
}
小程序服务器端:
import java.io.*;
import java.net.*;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class Server {
public static void main(String[]args){
try{
new MyWindows();
}catch(NullPointerException e2){}
}
}
class MyWindows extends JFrame implements ActionListener,WindowListener{
public static JTextField text1;
public static JTextArea text2;
public static JRadioButton [] radio=new JRadioButton[4];
public static Socket [] socket=new Socket[4];
ServerThread [] thread=new ServerThread[4];
public static ServerSocket server;
MyWindows(){
super("服务器程序");
String s1;
text1=new JTextField();
text2=new JTextArea();
this.setLayout(null);
JScrollPane jsp=new JScrollPane(text2);
jsp.setBounds(10,10,400,400);
text1.setBounds(10,420,400,30);
JPanel pan=new JPanel();
ButtonGroup g=new ButtonGroup();
for(int i=0;i<4;i++){
radio[i]=new JRadioButton("对话者"+i);
g.add(radio[i]);
pan.add(radio[i]);
}
radio[0].setSelected(true);
pan.setBounds(10,455,400,30);
this.add(jsp);this.add(text1);
this.add(pan);
this.setSize(420, 520);
this.setLocation(300,200);
this.setResizable(false);
this.setVisible(true);
text1.addActionListener(this);
this.addWindowListener(this);
while(true){
try{
server=new ServerSocket(4441);
}catch(IOException g1){
text2.append("读写出错aaaaa!!!"+"\n");
}
try{
for(int i=0;i<4;i++){
if(socket[i]==null){
socket[i]=server.accept();
thread[i]=new ServerThread(socket[i],i);
radio[i].setText(socket[i].getInetAddress().getHostAddress());
}
}
}catch(IOException g1){
text2.append("网络出错!!!"+"\n");
}
}
}
public void windowClosing(WindowEvent e){
for(int i=0;i<4;i++){
if(socket[i]!=null){
try{
thread[i].out.writeUTF("结束");
this.thread[i].socket.close();
socket[i].close();
}catch(IOException e1){}
}
}
try {
server.close();
} catch (IOException e1) {
}catch(NullPointerException e2){}
System.exit(1);
}
public void windowActivated(WindowEvent e){}
public void windowClosed(WindowEvent e){
for(int i=0;i<4;i++){
if(socket[i]!=null){
try{
this.thread[i].socket.close();
socket[i].close();
}catch(IOException e1){}
}
}
}
public void windowDeactivated(WindowEvent e){}
public void windowDeiconified(WindowEvent e){}
public void windowIconified(WindowEvent e){}
public void windowOpened(WindowEvent e){}
public void actionPerformed(ActionEvent e){
if(e.getSource()==text1){
for(int i=0;i<4;i++){
if(radio[i].isSelected()==true){
try {
thread[i].out.writeUTF(text1.getText());
text2.append(text1.getText()+"\n");
if(text1.getText().equals("结束")){
thread[i].socket.close();
socket[i].close();
socket[i]=null;
radio[i].setText("对话者"+i);
}
text1.setText("");
} catch (IOException e1) {
text2.append("读写出错bbbb!!!"+"\n");
}
}
}
}
}
}
class ServerThread extends Thread{
int i;
public String s;
public Socket socket;
public DataInputStream in;
public DataOutputStream out;
ServerThread(Socket soc,int j){
i=j;
socket=soc;
try{
in=new DataInputStream(socket.getInputStream());
out=new DataOutputStream(socket.getOutputStream());
}catch(IOException g){}
}
public void run(){
while(true){
try{
s=in.readUTF();
if(s.equals("结束")){
socket.close();
MyWindows.socket[i].close();
MyWindows.socket[i]=null;
MyWindows.radio[i].setText("对话者"+i);
break;
}
MyWindows.text2.append(s+"\n");
}catch(IOException g1){
}
}
}
}
怎样解决(请改代码)?
下个断点跟踪下啊######基本没人会看你这代码
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。