Socket编程小例子

socket编程客户端例子:

package com.gx.chat;

import java.awt.BorderLayout;
import java.awt.Frame;
import java.awt.TextArea;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
import java.net.UnknownHostException;

/**
* @author <a href="mailto:gaoxiang114@163.com">hover</a>
* @version 1.0.0.2014年4月8日
*/
public class CharClient extends Frame{

TextArea textArea = null;
TextField textField = null;
Socket socket = null;
DataOutputStream os = null;

public CharClient(){
setLocation(400, 300);
textArea = new TextArea();
textField = new TextField();
this.setSize(400, 400);
this.add(textArea,BorderLayout.NORTH);
this.add(textField,BorderLayout.SOUTH);
pack();
this.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
textField.addActionListener(new ActionListener() {

@Override
public void actionPerformed(ActionEvent e) {
String msg = textField.getText();
textArea.setText(msg);
textField.setText("");
try {
os.writeUTF(msg);
os.flush();
} catch (IOException e1) {
e1.printStackTrace();
}
}
});
this.setVisible(true);
connect();
}

public static void main(String[] args) {
new CharClient();
}

public void connect(){
try {
socket = new Socket("127.0.0.1",9999);
os = new DataOutputStream(socket.getOutputStream());
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
try {
if(os != null){
os.close();
}
if(socket != null){
socket.close();
}
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
}

 

 

 服务端例子:

package com.gx.chat;

import java.io.DataInputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;

/**
* @author <a href="mailto:gaoxiang114@163.com">hover</a>
* @version 1.0.0.2014年4月8日
*/
public class ChatServer {

ServerSocket serverSocket = null;
Socket socket = null;
boolean flag = false;
DataInputStream os = null;
public static void main(String[] args) {
new ChatServer().start();
}

public void start(){
try {
serverSocket = new ServerSocket(9999);
} catch (IOException e) {
e.printStackTrace();
System.exit(0);
}

flag = true;

try {
while(flag){
boolean isConnect = false;
socket = serverSocket.accept();
System.out.println("一个客户端连接");
new Thread(new ChatThread(socket)).start();;
}

} catch (IOException e) {
try {
os.close();
} catch (IOException e1) {
e1.printStackTrace();
}
}

}

class ChatThread implements Runnable{
Socket socket = null;
public ChatThread(Socket socket){
this.socket = socket;
}
boolean isConnect = false;
@Override
public void run() {
isConnect = true;
while(isConnect){
try {
os = new DataInputStream(socket.getInputStream());
System.out.println(os.readUTF());
} catch (IOException e) {
try {
os.close();
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
}
}
}

 

posted on 2014-04-08 16:36  持知以恒  阅读(150)  评论(0编辑  收藏  举报