一个端口可以让多个IP访问,是可以的。
但用一般的server端和client端是做不了的。做得了也不好。
你可以用socketchancel,或者双socket来做。
但用一般的server端和client端是做不了的。做得了也不好。
你可以用socketchancel,或者双socket来做。
解决方案 »
- hibernate+oracle报错(could not get next sequence value)
- ibatis的問題
- 问下各位大大,在北京5K---6K的java程序员要会到什么程度?
- j2ee -verbose 失败
- 谁能提供给我一个axis包,网站上下载不了!谢谢
- 如何在services服务器上放一个对象,来保存一些信息,就是类似于jsp中的application对象,请指教
- BMP EJB 问题集
- 哪位大虾帮忙看一下我这个小程序哪儿有问题?多谢了
- 请大家推荐一些XML方面的书!
- flash上传文件 java取不到
- Tomcat 5.5 中文乱码问题如何解决?
- java怎样计算两个日期间的差
* Created on 2004/11/25
*
* TODO To change the template for this generated file go to
* Window - Preferences - Java - Code Style - Code Templates
*/
package fts.chat;import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextArea;
import javax.swing.JTextField;/**
* @author zhl-wang
*
* TODO To change the template for this generated type comment go to
* Window - Preferences - Java - Code Style - Code Templates
*/
public class chatPanl extends JFrame implements ActionListener ,Runnable{
JTextArea m_textarea;
JTextField m_textfield;
JButton ok;
JButton no;
Container c;
DataInputStream m_in;
DataOutputStream m_out;
public chatPanl(){
super("聊天程序");
m_in = null;
m_out = null;
c = getContentPane();
setSize(426,266);
m_textarea = new JTextArea(10,100);
m_textfield = new JTextField("",100);
ok = new JButton("send");
no = new JButton("cancel");
try{
Socket m_socket;
m_socket = new Socket("192.168.151.72",5555);
m_in = new DataInputStream(m_socket.getInputStream());
m_out = new DataOutputStream(m_socket.getOutputStream());
}
catch (Exception e){
System.out.println("Error:"+e);
}
new Thread(this).start();
c.setLayout(new BorderLayout());
c.add(m_textarea,BorderLayout.SOUTH);
c.add(m_textfield,BorderLayout.CENTER);
c.add(ok,BorderLayout.EAST);
c.add(no,BorderLayout.WEST);
ok.addActionListener(this);
no.addActionListener(this);
m_textarea.setEditable(false);
show();
} public static void main(String[] args) {
chatPanl app = new chatPanl();
app.addWindowListener(new MyWindowListener());
} /* (non-Javadoc)
* @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
*/
public void actionPerformed(ActionEvent e) {
if (e.getSource()==ok){
String v = m_textfield.getText();
m_textfield.setText("");
try {
m_out.writeUTF(v);
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
else if(e.getSource()==no){
m_textfield.setText("");
}
} /* (non-Javadoc)
* @see java.lang.Runnable#run()
*/
public void run() {
try{
while (true){
String s = m_in.readUTF();
if (s!=null){
m_textarea.append(s+"\n");
}
}
}
catch (Exception e){
m_textarea.append("Bye welcome next enter the chat");
m_textfield.setVisible(false);
}
}
public void stop(){
try{
m_out.writeUTF("leave");
}
catch (IOException e){
}
}
}
/*
* Created on 2004/11/24
*
* TODO To change the template for this generated file go to
* Window - Preferences - Java - Code Style - Code Templates
*/
package fts.chat;import java.net.ServerSocket;
import java.net.Socket;
import java.util.Vector;/**
* @author zhl-wang
*
* TODO To change the template for this generated type comment go to
* Window - Preferences - Java - Code Style - Code Templates
*/
public class chatserver {
public static void main(String args[]){
ServerSocket socket = null;
Vector m_threads = new Vector();
System.out.println("Listen...");
try{
socket = new ServerSocket(5555);
}
catch (Exception e){
System.out.println("new ServerSocket() failed!");
return;
}
try{
int nid = 0;
while(true){
Socket s = socket.accept();
System.out.println("accepted");
ServerThread st = new ServerThread(s,m_threads);
st.setID(nid++);
m_threads.addElement(st);
new Thread(st).start();
for (int i=0;i<m_threads.size();i++){
ServerThread st1 = (ServerThread)m_threads.elementAt(i);
st1.write("<#>welcome "+"to enter chatroom");
}
System.out.println("Listen again...");
}
}
catch (Exception e){
System.out.println("Server is down");
}
}
}
thread/*
* Created on 2004/11/24
*
* TODO To change the template for this generated file go to
* Window - Preferences - Java - Code Style - Code Templates
*/
package fts.chat;import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
import java.util.Vector;/**
* @author zhl-wang
*
* TODO To change the template for this generated type comment go to
* Window - Preferences - Java - Code Style - Code Templates
*/
public class ServerThread implements Runnable {
Vector m_threads;
Socket m_socket = null;
DataInputStream m_in = null;
DataOutputStream m_out = null;
int m_nid;
public ServerThread(Socket s, Vector threads){
m_socket = s;
m_threads = threads;
try{
m_in = new DataInputStream(m_socket.getInputStream());
m_out = new DataOutputStream(m_socket.getOutputStream());
}
catch (Exception e){
}
} /* (non-Javadoc)
* @see java.lang.Runnable#run()
*/
public void run() {
System.out.println("thread is running");
boolean flag = true;
try{
while (flag){
String s = m_in.readUTF();
if (s==null) break;
if (s.intern().equals("leave")){
for (int i=0;i<m_threads.size();i++){
ServerThread st = (ServerThread)m_threads.elementAt(i);
st.write("***"+getID()+"leave..."+"***");
flag = false;
}
}
else{
for (int i=0;i<m_threads.size();i++){
ServerThread st = (ServerThread)m_threads.elementAt(i);
st.write("<"+getID()+">"+s);
}
}
}
}
catch (Exception e){
}
m_threads.removeElement(this);
try{
m_socket.close();
}
catch (Exception e){
System.out.println("Connect is dowm");
}
}
public void write(String msg){
synchronized(m_out){
try{
m_out.writeUTF(msg);
}
catch (IOException e){
}
}
}
public int getID(){
return m_nid;
}
public void setID(int nid){
m_nid = nid;
}
}