標題:
server and client
[打印本頁]
作者:
零售商
時間:
2012-3-30 21:32
標題:
server and client
本帖最後由 零售商 於 2012-3-31 15:05 編輯
有些地方還沒有搞懂,此程式參照:巨匠電腦花蓮分校java ocjp實做班,範例
import java.net.*;
import java.io.*;
import java.util.*;
public class MSNServer {
public static Vector v = new Vector();
public MSNServer() {
}
public static void main(String[] args) throws IOException{
ServerSocket ss = new ServerSocket(8080);
while(true){
Socket cs = ss.accept();
GetConnect gc = new GetConnect(cs , v);
Thread t = new Thread(gc);
t.start();
}
}
}
class GetConnect implements Runnable {
GetConnect(Socket cs , Vector v){
this.cs = cs;
this.v = v;
}
private Socket cs;
private Vector v;
BufferedReader br;
PrintWriter out;
String name = "";
String getMsg2;
public void run(){
try{
br = new BufferedReader(new InputStreamReader(cs.getInputStream()));
out = new PrintWriter(cs.getOutputStream() , true);
out.println("歡迎光臨");
out.println("請輸入暱稱:");
v.add(this);
name = br.readLine();
SendMsg("In");
while(true){
String getMsg = br.readLine();
SendMsg(getMsg);
if(getMsg == null){
br.close();
break;
}
}
cs.close();
ExitConnect();
}
catch(IOException ie){
}
}
void SendMsg(String getMsg) {
try{
ListIterator it = v.listIterator();
while(it.hasNext()){
getMsg2 = getMsg;
GetConnect gc = (GetConnect)it.next();
PrintWriter pw = new PrintWriter(gc.cs.getOutputStream() , true);
if(getMsg2 == "In")
getMsg2 = name + "已登入";
else if(getMsg2 == "Out")
getMsg2 = name + "已登出";
else
getMsg2 = name + "說:" + getMsg2;
pw.println(getMsg2);
}
System.out.println(getMsg2);
}
catch(IOException ie){
}
}
void ExitConnect(){
v.remove(this);
SendMsg("Out");
}
}
複製代碼
import java.awt.*;
import javax.swing.*;
import java.util.*;
import java.net.*;
import java.io.*;
import java.awt.event.*;
public class MSNClient {
public MSNClient() {
}
public static void main(String[] args) {
MSNMessage m = new MSNMessage();
}
}
class MSNMessage implements KeyListener {
JFrame jfMSN = new JFrame("即時通訊");
TextArea taList = new TextArea(10,30);
JPanel jp = new JPanel();
JTextField jtfSend = new JTextField();
BufferedReader br;
PrintWriter pw;
boolean UserIn = false;
MSNMessage(){
Container c = jfMSN.getContentPane();
jfMSN.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
c.setLayout(new BorderLayout());
jfMSN.setLocation(300,200);
jfMSN.setSize(500,500);
taList.setFont(new Font("新細明體",Font.BOLD,24));
taList.setFocusable(false);
jtfSend.setFont(new Font("新細明體",Font.BOLD,24));
jp.setBorder(BorderFactory.createTitledBorder("請輸入傳送的訊息:(按 Enter 送出)"));
jp.setLayout(new GridLayout(1,1));
jp.add(jtfSend);
c.add("Center",taList);
c.add("South",jp);
jtfSend.addKeyListener(this);
jfMSN.setVisible(true);
try{
Socket s = new Socket();
s.bind(null);
s.connect(new InetSocketAddress("192.168.1.68",8080),1000);
br = new BufferedReader (new InputStreamReader(s.getInputStream()));
pw = new PrintWriter(s.getOutputStream(),true);
while(true){
String getMsg = br.readLine();
taList.append(getMsg + "\n");
}
}
catch(Exception e){
}
}
public void keyPressed(KeyEvent ke){
if(ke.getKeyCode() == 10){
pw.println(jtfSend.getText());
if(UserIn == false){
jfMSN.setTitle("即時通訊 目前使用者:" + jtfSend.getText());
UserIn = true;
}
jtfSend.setText("");
}
}
public void keyTyped(KeyEvent ke){
}
public void keyReleased(KeyEvent ke){
}
}
複製代碼
作者:
waynechen251
時間:
2012-5-27 16:58
你要不要用java寫要地聊天室呢= =
作者:
零售商
時間:
2012-5-27 18:00
回復
2#
waynechen251
網頁語法我沒有學欸...這是程式的
作者:
waynechen251
時間:
2012-7-31 23:01
至於如何將程式碼封裝成軟體你可不可以教導我?
歡迎光臨 戰略要地 (http://urbase.net/bbs2/)
Powered by Discuz! 7.2