Java实现简单的socket通信教程
今天学习了一下java如何实现socket通信,感觉难点反而是在io上,因为java对socket封装已经很完善了。
今天代码花了整个晚上调试,主要原因是io的flush问题和命令行下如何运行具有package的类,不过最后问题基本都解决了,把代码贴出来供大家参考
server
public class TcpServer { public static void main(String[] args) throws Exception { ServerSocket server = new ServerSocket(9091); try { Socket client = server.accept(); try { BufferedReader input = new BufferedReader(new InputStreamReader(client.getInputStream())); boolean flag = true; int count = 1; while (flag) { System.out.println(客户端要开始发骚了,这是第 + count + 次!); count++; String line = input.readLine(); System.out.println(客户端说: + line); if (line.equals(exit)) { flag = false; System.out.println(客户端不想玩了!); } else { System.out.println(客户端说: + line); } } } finally { client.close(); } } finally { server.close(); } } }
client
public class TcpClient { public static void main(String[] args) throws Exception { Socket client = new Socket(127.0.0.1, 9091); try { PrintWriter output = new PrintWriter(client.getOutputStream(), true); Scanner cin = new Scanner(System.in); String words; while (cin.hasNext()) { words = cin.nextLine(); output.println(words); System.out.println(写出了数据: + words); } cin.close(); } finally { client.close(); } } }
补充知识:Java-Socket通信 同时JSON传递与解析
服务端
import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.net.ServerSocket; import java.net.Socket; import java.util.Scanner; import net.sf.json.JSONObject; public class Server_2 { public static void main(String[] args) { Scanner in=new Scanner(System.in); InputStreamReader isr; BufferedReader br; OutputStreamWriter osw; BufferedWriter rw; try { ServerSocket serverSocket=new ServerSocket(4444); Socket socket=serverSocket.accept(); isr=new InputStreamReader(socket.getInputStream()); br=new BufferedReader(isr); String str=br.readLine(); JSONObject object=JSONObject.fromObject(str); System.out.println("ID:"+object.getInt("ID")); System.out.println("Name:"+object.getString("name")); System.out.println("password:"+object.getString("password")); br.close(); socket.close(); serverSocket.close(); } catch (IOException e) { e.printStackTrace(); } } }
客服端
import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.net.Socket; import java.util.Scanner; import net.sf.json.JSONObject; public class Client_2 { public static void main(String[] args) { Scanner in = new Scanner(System.in); InputStreamReader isr; BufferedReader br; OutputStreamWriter osw; BufferedWriter rw; try { Socket socket = new Socket("localhost", 4444); osw = new OutputStreamWriter(socket.getOutputStream()); rw = new BufferedWriter(osw); User user = new User(); System.out.println("Id:"); user.setID(in.nextInt()); in.nextLine(); System.out.println("Name:"); user.setName(in.nextLine()); System.out.println("Password:"); user.setPassword(in.nextLine()); JSONObject jsonObject = JSONObject.fromObject(user); rw.write(jsonObject.toString()+"\n"); rw.close(); socket.close(); } catch (Exception e) { // TODO: handle exception } } }
User
public class User { int ID; String name; String password; public int getID() { return ID; } public void setID(int iD) { ID = iD; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } }
以上这篇Java实现简单的socket通信教程就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持我们。
赞 (0)