看看Think in java的网络编程那章吧,这个问题简直就是针对它出的。
解决方案 »
- jpa单向多对多关联,无法插入中间表数据
- ssh集成 action不能用求大神指教在线等很着急
- 通过超链接 提交表单
- JAVA Web编程中Oracle SQL的调优及表、索引的设计和优化
- spring的解耦合,如何不修改代码的情况下能不能改变接口的参数
- junit测试hibernate问?
- 请问Tomcat是web服务器还是Servlet容器或者两者都是???
- [求助]我用STRUTS做的搜寻页面不能查询条件值不能为中文?
- 想起一个问题(XML排序---讨论一下)!
- 那位有STRUTS的中文资料及电子书?
- struts1.2问题
- 2003的服务器,为什么我启动tomcat之后,在不到2秒的时间就自动关闭。
import java.net.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.Timer;
public class ChattingServer {
static final int PORT = 8080;
public static void main(String[] args)
throws IOException {
ToAll toall=new ToAll();
ServerSocket s = new ServerSocket(PORT);
System.out.println("Server Started");
try {
while(true) {
Socket socket = s.accept();
try {
ServeOneJabber server=new ServeOneJabber(socket);
toall.count++;
toall.temp=toall.count;
server.start();
} catch(IOException e) {
socket.close();
}
System.out.println(toall.count);
}
} finally {
s.close();
}
}
}
class ServeOneJabber extends Thread{
public ServeOneJabber(Socket s)
throws IOException{
socket=s;
send=new ToAll();
in=
new BufferedReader(
new InputStreamReader(
socket.getInputStream()));
send.out[send.count]=
new PrintWriter(
new BufferedWriter(
new OutputStreamWriter(
socket.getOutputStream())),true);
}
public void run(){
receiveThread receivethread=new receiveThread();
receivethread.start();
sendThread sendthread=new sendThread();
sendthread.start();
}
private class receiveThread extends Thread{
public void run(){
try{
try{
while(true){
String str=in.readLine();
// if(str.equals("end")) break;
send.strcon=str;
System.out.println(str);
sleep(150);
}
}
catch(InterruptedException e){
}
System.out.println("closing...");
}
catch(IOException e){
}finally{
try{
socket.close();
}
catch(IOException e){
}
}
}
}
private class sendThread extends Thread{
public void run(){
try{
while(true){
if(send.strcon!=null){
send.temp=send.count;
for(int i=0;i<send.temp;i++){
send.out[i].println(send.strcon);
}
send.strcon=null;
}
sleep(100);
}
}
catch(InterruptedException e){
}
}
} private class TimeEvent implements ActionListener{
public void actionPerformed(ActionEvent event){
send.strcon=null;
}
}
private ToAll send;
private Socket socket;
private BufferedReader in;
public static PrintWriter out[];
}
class ToAll {
public static int count=0;
public static int temp;
public static String strcon;
public static PrintWriter out[]=new PrintWriter[10];
}
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
import java.net.*;public class ChattingClient{
public static void main(String[] args){
try{
RoomFrame frame=new RoomFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.show();
}
catch(IOException e){
}
}
}class RoomFrame extends JFrame{
public RoomFrame()
throws IOException {
Toolkit kit=Toolkit.getDefaultToolkit();
Dimension screenSize=kit.getScreenSize();
setTitle("聊天室");
setSize(screenSize.height/2,screenSize.width/2);
setLocation(screenSize.height/2,screenSize.width/8);
send=new JButton("发送");
sendContent=new JTextField(27);
sendPanel=new JPanel();
sendPanel.add(sendContent);
sendPanel.add(send);
room=new JTextArea();
JScrollPane scrollPane=new JScrollPane(room);
sendAction senda=new sendAction();
send.addActionListener(senda);
Container contantPane=getContentPane();
contantPane.add(sendPanel,BorderLayout.SOUTH);
contantPane.add(scrollPane,BorderLayout.CENTER);
room.setEditable(false);
room.setLineWrap(true);
InputMap imap=sendPanel.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
imap.put(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER,Event.CTRL_MASK),"sendkey");
ActionMap amap=sendPanel.getActionMap();
amap.put("sendkey",senda);
//connection
InetAddress addr =
InetAddress.getByName(null);
try{
socket = new Socket(addr, 8080);
socket.setSoTimeout(10000); System.out.println("socket = " + socket);
in =
new BufferedReader(
new InputStreamReader(
socket.getInputStream()));
out =
new PrintWriter(
new BufferedWriter(
new OutputStreamWriter(
socket.getOutputStream())),true);
}
catch(IOException e){
}
receiveThread receive=new receiveThread();
receive.start();
}
private class receiveThread extends Thread{
public void run(){
try{
while(true){
try{
String str = in.readLine();
room.append(str+'\n');
}
catch(IOException e){
}
sleep(1000);
}
}
catch(InterruptedException exception){
}
}
}
private class sendAction extends AbstractAction{
public void actionPerformed(ActionEvent evt){
out.println(sendContent.getText());
sendContent.setText("");
}
}
private BufferedReader in;
private PrintWriter out;
private Socket socket;
private JTextArea room;
private JPanel sendPanel;
private JTextField sendContent;
private JButton send;
}