关于socket介绍
一、介绍大概编写socket网络的基本步骤
1、编写socket需要两个主类:Client和Server类,所以先创建两个类
2)先编写server类,在主函数中创建ServerSocket对象,用一个socket对象来接收等待消息,再用一个线程去启动主要的客户端发来的消息,当然要用输入、输出流来接收信息
3)在ServerThread中用构造方法将socket传过来,在其构造方法中建立输入输出流并将其流包装成缓冲流
4)线程中重写run方法,用文件输入流读取客户端信息,然后将读到的信息进行处理
5)最后在server类中一定要将线程启动
2、编辑client客户端
1)客户端与服务端代码有多相似之处,先在client中创建socket对象,与服务端的socket相对应,用一个线程来启动输入输出流,并在线程中创建方法用输出流来给服务端进行传输信息,记得要在socket中传IP地址和端口。
例:1、留言板
Client类:
package com.huaxin.lyb2;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.Socket;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextField;
public class Client extends JFrame {
public static void main(String[] args) {
Client c = new Client();
c.showUI();
}
private void showUI() {
setTitle("我的客户端");
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setSize(300, 200);
final JTextField jtf = new JTextField();
JButton jb = new JButton("发送");
add(jtf);
add(jb, BorderLayout.SOUTH);
jb.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
String msg = jtf.getText();
Socket socket = new Socket("192.168.0.107", 8888);
OutputStream os = socket.getOutputStream();
OutputStreamWriter osw = new OutputStreamWriter(os);
BufferedWriter bw = new BufferedWriter(osw);
bw.write(msg);
bw.newLine();
bw.flush();
jtf.setText("");
os.close();
osw.close();
bw.close();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
});
setVisible(true);
}
}
server端:
package com.huaxin.lyb2;
import java.net.ServerSocket;
import java.net.Socket;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
public class Server extends JFrame {
public static void main(String[] args) {
Server sr = new Server();
sr.showUI();
sr.StartRun();
}
private JTextArea jta;
private void StartRun() {
new Thread() {
public void run() {
try {
ServerSocket ss = new ServerSocket(8888);
while (true) {
Socket socket = ss.accept();
ServerThread st = new ServerThread(socket, Server.this);
st.start();
}
} catch (Exception e) {
e.printStackTrace();
}
};
}.start();
}
private void showUI() {
setTitle("liuyanban");
setDefaultCloseOperation(EXIT_ON_CLOSE);
setSize(300, 500);
setLocationRelativeTo(null);
jta = new JTextArea();
jta.setEditable(false);
jta.setLineWrap(true);
JScrollPane jsp = new JScrollPane(jta);
jsp.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
jsp.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
add(jsp);
setVisible(true);
}
public void showMsg(String msg) {
long currTimeMills = System.currentTimeMillis();
Date date = new Date(currTimeMills);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String time = sdf.format(date);
jta.append(time + "\r\n" + msg);
jta.append("\r\n");
}
}
severThread类:
package com.huaxin.lyb2;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.Socket;
public class ServerThread extends Thread {
private BufferedReader br;
public Server sr;
Socket socket;
public ServerThread(Socket socket, Server sr) {
try {
InputStream is = socket.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
br = new BufferedReader(isr);
} catch (Exception e) {
e.printStackTrace();
}
this.sr = sr;
this.socket = socket;
}
@Override
public void run() {
try {
String readline = br.readLine();
String ip = socket.getInetAddress().toString();
sr.showMsg(ip + "说" + readline);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
2、聊天室