主题:Socket网络编程 实现客户端和服务器的步骤是什么啊
zheng0510yi
[专家分:0] 发布于 2008-09-07 16:20:00
Socket网络编程 实现客户端和服务器的步骤是什么啊
能不能把具体程序实现发一下
最后更新于:2008-09-07 19:08:00
回复列表 (共2个回复)
沙发
happyboy2007 [专家分:3900] 发布于 2008-09-08 14:14:00
服务器端代码
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.ServerSocket;
import java.net.Socket;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
public class Server extends JFrame implements ActionListener
{
private Socket s;
private ServerSocket ss;
private BufferedReader br;
private BufferedWriter bw;
private JTextField tf = new JTextField();
private JTextArea ta = new JTextArea();
private JScrollPane sp = new JScrollPane(ta,JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
public Server()
{
try
{
this.getContentPane().add(tf,"North");
this.getContentPane().add(sp,"Center");
tf.addActionListener(this);
this.setTitle("服务器");
this.setSize(400,400);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setResizable(false);
this.setVisible(true);
ss = new ServerSocket(9000);
s = ss.accept();
br = new BufferedReader(new InputStreamReader(s.getInputStream()));
bw = new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
new ServerReader().start();
}
catch (Exception e)
{
e.printStackTrace();
}
}
class ServerReader extends Thread
{
public void run()
{
try
{
while(true)
{
String str = null;
if((str=br.readLine())!=null)
ta.append(str+"\r\n");
}
}
catch (Exception e)
{
}
}
}
public static void main(String[] args)
{
new Server();
}
public void actionPerformed(ActionEvent e)
{
try
{
bw.write(tf.getText()+"\r\n");
bw.flush();
tf.setText("");
}
catch (Exception ex)
{
}
}
}
板凳
happyboy2007 [专家分:3900] 发布于 2008-09-08 14:14:00
客户端代码
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.Socket;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
public class Client extends JFrame implements ActionListener
{
private Socket s;
private BufferedReader br;
private BufferedWriter bw;
private JTextField tf = new JTextField();
private JTextArea ta = new JTextArea();
private JScrollPane sp = new JScrollPane(ta,JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
public Client()
{
try
{
this.getContentPane().add(tf,"North");
this.getContentPane().add(sp,"Center");
tf.addActionListener(this);
this.setTitle("客户端");
this.setSize(400,400);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setResizable(false);
this.setVisible(true);
s = new Socket("127.0.0.1",9000);
br = new BufferedReader(new InputStreamReader(s.getInputStream()));
bw = new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
new ClientReader().start();
}
catch (Exception e)
{
e.printStackTrace();
}
}
class ClientReader extends Thread
{
public void run()
{
try
{
while(true)
{
String str = null;
if((str=br.readLine())!=null)
ta.append(str+"\r\n");
}
}
catch (Exception e)
{
}
}
}
public static void main(String[] args)
{
new Client();
}
public void actionPerformed(ActionEvent e)
{
try
{
bw.write(tf.getText()+"\r\n");
bw.flush();
tf.setText("");
}
catch (Exception ex)
{
}
}
}
我来回复