大家看看]
这是一个关于JAVA Socket一对一聊天工具:
分客户端和服务器端:
客户端可以发消息到服务器端
但是反过来不可以
如果我把下面程序中标有/**AAAAAAAAA***/ 和标有/**CCCCCCCCCCCCC**/
它们互换注释 情况又是相反的结果??
有点奇怪
高手看看 错在哪......
小第先谢谢了...
////服务器端
package GG工程;
import java.io.*;
import java.net.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Server extends JFrame
{
private JTextArea jTextArea;
private JTextField jTextField;
InputStream inputStream;
OutputStream outputStream;
static ServerSocket serverSocket;
Socket socket;
public Server()
{
////窗体///////////////////////////////////////
JFrame.setDefaultLookAndFeelDecorated(true);
JFrame jFrame=new JFrame("GG服务器端");//新建窗体
jFrame.setBounds(200, 200, 320, 300);
jFrame.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent we)
{
System.exit(0);
}
});
/////文本域//////////////////////////////////////
jTextArea=new JTextArea();//新建文本域
jTextArea.setEditable(false);//设置不可编辑属性
JScrollPane jScrollPane=new JScrollPane(jTextArea);
jFrame.getContentPane().add(jScrollPane,"Center");//添加到窗体
//////面版和按钮属性事件//////////////////////////////////
JPanel jPanel_1=new JPanel();
JLabel jLabel=new JLabel("请输入消息");
jTextField=new JTextField(20);
jPanel_1.add(jLabel);
jPanel_1.add(jTextField);
JPanel jPanel_2=new JPanel();
JButton jButton_1=new JButton("发送");
jButton_1.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent ae)
{
jTextArea.append("\n你说:"+jTextField.getText());
connetcSend();
jTextField.setText("");
}
});
JButton jButton_2=new JButton("取消");
jButton_2.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent ae)
{
jTextField.setText("");
}
});
JButton jButton_3=new JButton("退出");
jButton_3.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent ae)
{
System.exit(0);
}
});
jPanel_2.add(jButton_1);
jPanel_2.add(jButton_2);
jPanel_2.add(jButton_3);
JPanel jPanel_12=new JPanel(new GridLayout(2,1));
jPanel_12.add(jPanel_1);
jPanel_12.add(jPanel_2);
jFrame.getContentPane().add(jPanel_12,"South");
jFrame.show();
}
public void connectRecive()
{
try
{
String str;
socket=serverSocket.accept();
while(true)
{
inputStream=socket.getInputStream();
BufferedInputStream bufferInput=new BufferedInputStream(inputStream);
byte[] bty=new byte[100];
int len=bufferInput.read(bty);
str=new String(bty,0,len);
jTextArea.append("对方说:"+str+"\n");
if(str.equals("exit"))
break;
}
inputStream.close();
socket.close();
}
catch(Exception e)
{
e.printStackTrace();
}
}
public void connetcSend()
{
try
{
socket=serverSocket.accept(); /**AAAAAAAAAAAAAAAAAAAAA**/
outputStream=socket.getOutputStream();
BufferedOutputStream bufferOutput=new BufferedOutputStream(outputStream);
bufferOutput.write(jTextField.getText().getBytes());
bufferOutput.flush();
}
catch(Exception e)
{
e.printStackTrace();
}
}
public static void main(String [] args) throws Exception
{
serverSocket=new ServerSocket(6007);
Server server=new Server();
//socket=serverSocket.accept(); /**CCCCCCCCCCCCCCCCCCCCCC**/
server.connectRecive();
}
}
/////////客户端
package GG工程;
import java.io.*;
import java.net.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Client extends JFrame
{
private JTextArea jTextArea;
private JTextField jTextField;
InputStream inputStream;
OutputStream outputStream;
static Socket socket;
public Client()
{
////窗体///////////////////////////////////////
JFrame.setDefaultLookAndFeelDecorated(true);
JFrame jFrame=new JFrame("GG客户端");//新建窗体
jFrame.setBounds(200, 200, 320, 300);
jFrame.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent we)
{
System.exit(0);
}
});
/////文本域//////////////////////////////////////
jTextArea=new JTextArea();//新建文本域
jTextArea.setEditable(false);//设置不可编辑属性
JScrollPane jScrollPane=new JScrollPane(jTextArea);
jFrame.getContentPane().add(jScrollPane,"Center");//添加到窗体
//////面版和按钮属性事件//////////////////////////////////
JPanel jPanel_1=new JPanel();
JLabel jLabel=new JLabel("请输入消息");
jTextField=new JTextField(20);
jPanel_1.add(jLabel);
jPanel_1.add(jTextField);
JPanel jPanel_2=new JPanel();
JButton jButton_1=new JButton("发送");
jButton_1.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent ae)
{
jTextArea.append("\n你说:"+jTextField.getText());
connetcSend();
jTextField.setText("");
}
});
JButton jButton_2=new JButton("取消");
jButton_2.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent ae)
{
jTextField.setText("");
}
});
JButton jButton_3=new JButton("退出");
jButton_3.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent ae)
{
System.exit(0);
}
});
jPanel_2.add(jButton_1);
jPanel_2.add(jButton_2);
jPanel_2.add(jButton_3);
JPanel jPanel_12=new JPanel(new GridLayout(2,1));
jPanel_12.add(jPanel_1);
jPanel_12.add(jPanel_2);
jFrame.getContentPane().add(jPanel_12,"South");
jFrame.show();
}
public void connectRecive()
{
//Socket socket;
try
{
String str;
while(true)
{
//socket=new Socket("127.0.0.1",6007);
inputStream=socket.getInputStream();
BufferedInputStream bufferInput=new BufferedInputStream(inputStream);
byte[] bty=new byte[100];
int len=bufferInput.read(bty);
str=new String(bty,0,len);
jTextArea.append("对方说:"+str+"\n");
if(str.equals("exit"))
break;
}
inputStream.close();
socket.close();
}
catch(Exception e)
{
e.printStackTrace();
}
}
public void connetcSend()
{
try
{
outputStream=socket.getOutputStream();
BufferedOutputStream bufferOutput=new BufferedOutputStream(outputStream);
bufferOutput.write(jTextField.getText().getBytes());
bufferOutput.flush();
}
catch(Exception e)
{
e.printStackTrace();
}
}
public static void main(String [] args) throws Exception
{
Client client=new Client();
socket=new Socket("127.0.0.1",6007);
client.connectRecive();
}
}
[此贴子已经被作者于2007-10-12 12:49:22编辑过]