在《Java从入门到精通(第3版)》的原书中,客户端仅能发送一次数据,我在此基础上修改了一点点,实现了多次发送数据的单向通讯。
1. 服务器端
package Tcp_IP; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.net.ServerSocket; import java.net.Socket; import java.sql.Date; import java.sql.Time; import java.text.SimpleDateFormat; import java.util.SimpleTimeZone; import com.sun.org.apache.bcel.internal.generic.NEW; import javafx.beans.binding.When; import IO_Test.bufferread; public class My_Server { private BufferedReader reader; private ServerSocket Ser_Sock; private Socket socket; void My_Server() { // TODO Auto-generated constructor stub try { Ser_Sock = new ServerSocket(8766); System.out.println("等待客户连接中···"); socket = Ser_Sock.accept(); reader = new BufferedReader (new InputStreamReader(socket.getInputStream())); while(reader!=null){ SimpleDateFormat forma = new SimpleDateFormat("yyyy-mm-dd hh:mm:ss"); System.out.println("连接成功:"+forma.format(new java.util.Date())); System.out.println("获得客户端信息为:"+reader.readLine()); } reader.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public static void main(String[] args) { // TODO Auto-generated method stub My_Server SV = new My_Server(); SV.My_Server(); } }
2. 客户端
package Tcp_IP; import java.awt.BorderLayout; import java.awt.Container; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.io.IOException; import java.io.PrintWriter; import java.net.Socket; import java.net.UnknownHostException; import javax.swing.JFrame; import javax.swing.JScrollPane; import javax.swing.JTextArea; import javax.swing.JTextField; import javax.swing.border.BevelBorder; public class My_Client extends JFrame{ Socket socket; private JTextArea ta = new JTextArea(); private JTextField tf = new JTextField(); private PrintWriter writer; Container c; public My_Client(String titl) { // TODO Auto-generated constructor stub super(titl); setDefaultCloseOperation(EXIT_ON_CLOSE); c = getContentPane(); final JScrollPane scollpane = new JScrollPane(); scollpane.setBorder(new BevelBorder(BevelBorder.RAISED)); getContentPane().add(scollpane, BorderLayout.CENTER); scollpane.setViewportView(ta); c.add(tf, "South"); ta.append("尝试连接\n"); try { socket = new Socket("127.0.0.1", 8766); } catch (UnknownHostException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } catch (IOException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } ta.append("已连接,请输入发送信息\n"); tf.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub try { writer = new PrintWriter(socket.getOutputStream(), true); writer.println(tf.getText()); ta.append(tf.getText()+"\n"); ta.setSelectionEnd(tf.getText().length()); tf.setText("");} catch (IOException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } } }); } public static void main(String[] args) { // TODO Auto-generated method stub My_Client client = new My_Client("连接服务器"); client.setSize(200, 200); client.setVisible(true); } }
3. 运行效果
版权声明:本文为博主原创文章,未经博主允许不得转载。
时间: 2024-10-18 05:37:59