socket ( java ) 簡易用戶端、服務端通訊

來源:互聯網
上載者:User
原創作品,允許轉載,轉載時請務必以超連結形式標明文章 原始出處 、作者資訊和本聲明。否則將追究法律責任。http://blog.csdn.net/love254443233/article/details/7881886

實現:

用戶端:用兩個socket(兩個連接埠),一個用於接收服務端發送過來的訊息,一個用於向服務端發送訊息。

服務端:用兩個socket,一個用於接收用戶端發送過來的訊息,一個用於接收訊息手自動向用戶端發送訊息。

註:可單獨運行SocketServer與SocketClient;

1、SocketServer類:

package org.jivesoftware.openfire.plugin.test;import java.io.IOException;import java.io.InputStream;import java.io.OutputStream;import java.net.ServerSocket;import java.net.Socket;import java.net.UnknownHostException;public class SocketServer implements Runnable {public int hostPort = 5000;public int remotePort = 5001;public boolean sendMessage=false;public String remoteAddress="localhost";public static void main(String args[]){SocketServer socketServer=new SocketServer();socketServer.run();}public void run() {Socket socket=null;ServerSocket server=null;InputStream is=null;OutputStream os=null;try { server = new ServerSocket(hostPort); socket = server.accept();System.out.println(socket);is = socket.getInputStream();byte[] bytes;while (true) {bytes = new byte[2048];int n = is.read(bytes);System.out.print(new String(bytes, 0, n));if(sendMessage)sendMessage();}} catch (Exception e) {e.printStackTrace();}try {if (socket != null)socket.close();if(is!=null)is.close();if(os!=null)os.close();if(server!=null)server.close();} catch (IOException e) {e.printStackTrace();}}public void sendMessage() {Socket socket = null;OutputStream outputStream = null;int i = 0;String string = "這裡是伺服器。。。";try {socket = new Socket(remoteAddress, remotePort);outputStream = socket.getOutputStream();outputStream.write((string + (i++)+"\n").getBytes());outputStream.flush();} catch (UnknownHostException e1) {try {if (socket != null)socket.close();if (outputStream != null)outputStream.close();} catch (IOException e) {e.printStackTrace();}System.out.println("伺服器連接埠沒開。。");e1.printStackTrace();} catch (IOException e1) {e1.printStackTrace();}}}

2、SocketClient類:

package org.jivesoftware.openfire.plugin.test;import java.io.IOException;import java.io.OutputStream;import java.net.Socket;import java.net.UnknownHostException;import java.util.Date;public class SocketClient implements Runnable {public static void main(String args[]) {SocketClient socketClient = new SocketClient();socketClient.run();}public int remotePort = 5000;public String remoteAddress = "localhost";public SocketClient() {}public void run() {Socket socket = null;OutputStream outputStream = null;try {socket = new Socket(remoteAddress, remotePort);outputStream = socket.getOutputStream();outputStream.write(("connect to the server at time " + new Date().toString()+"\n").getBytes());outputStream.flush();System.out.println(socket);byte[] buffer;int size = 0;while (true) {System.out.println("input:");buffer = new byte[2048];size = System.in.read(buffer);if (size > 0) {outputStream.write(new String(buffer, 0, size).getBytes());outputStream.flush();}}} catch (UnknownHostException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}try {if (socket != null)socket.close();if (outputStream != null)outputStream.close();} catch (IOException e) {e.printStackTrace();}}}

3、TCPServer類:

package org.jivesoftware.openfire.plugin.test;import socket.SocketServer;public class TCPServer {private static SocketServer socketServer;public static void main(String[] args){socketServer = new SocketServer();socketServer.hostPort=5000;socketServer.remotePort=5001;socketServer.sendMessage=true;Thread serverThread = new Thread(socketServer);/*socketClient = new SocketClient();socketClient.remotePort=5001;//socketClient.setStr("TCPServer:");Thread clienThread = new Thread(socketClient);clienThread.start();*/serverThread.start();System.out.println("伺服器啟動完成:");}}

4、TCPClent類:

package org.jivesoftware.openfire.plugin.test;import socket.SocketClient;import socket.SocketServer;public class TCPClent {private static SocketServer socketServer;private static SocketClient socketClient;public static void main(String[] args) {socketServer = new SocketServer();socketServer.hostPort=5001;socketServer.remotePort=5000;socketServer.sendMessage=true;Thread serverThread = new Thread(socketServer);socketClient = new SocketClient();socketClient.remotePort=5000;Thread clienThread = new Thread(socketClient);clienThread.start();serverThread.start();System.out.println("伺服器啟動完成:");}}

本文方法存在一些問題,其改進版,見:http://blog.csdn.net/love254443233/article/details/7897269(多線程、多用戶端)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.