Today I learned how Java implements socket communication. I feel that the difficulty lies in iOS, because Java has already perfected socket encapsulation.
The code spent the whole night debugging today. The main reason is the flush problem of io and how to run a package class under the command line. However, the problem was basically solved in the end. Post the code for your reference.
server
public class TcpServer { public static void main(String[] args) throws Exception { ServerSocket server = new ServerSocket(9091); try { Socket client = (); try { BufferedReader input = new BufferedReader(new InputStreamReader(())); boolean flag = true; int count = 1; while (flag) { (The client is about to start to get slutty,This is the first + count + Second-rate!); count++; String line = (); (The client says: + line); if ((exit)) { flag = false; (The client doesn't want to play!); } else { (The client says: + line); } } } finally { (); } } finally { (); } } }
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((), true); Scanner cin = new Scanner(); String words; while (()) { words = (); (words); (Write data: + words); } (); } finally { (); } } }
Supplementary knowledge:Java-Socket communication Simultaneous JSON transmission and parsing
Server side
import ; import ; import ; import ; import ; import ; import ; import ; import ; public class Server_2 { public static void main(String[] args) { Scanner in=new Scanner(); InputStreamReader isr; BufferedReader br; OutputStreamWriter osw; BufferedWriter rw; try { ServerSocket serverSocket=new ServerSocket(4444); Socket socket=(); isr=new InputStreamReader(()); br=new BufferedReader(isr); String str=(); JSONObject object=(str); ("ID:"+("ID")); ("Name:"+("name")); ("password:"+("password")); (); (); (); } catch (IOException e) { (); } } }
Customer service
import ; import ; import ; import ; import ; import ; import ; public class Client_2 { public static void main(String[] args) { Scanner in = new Scanner(); InputStreamReader isr; BufferedReader br; OutputStreamWriter osw; BufferedWriter rw; try { Socket socket = new Socket("localhost", 4444); osw = new OutputStreamWriter(()); rw = new BufferedWriter(osw); User user = new User(); ("Id:"); (()); (); ("Name:"); (()); ("Password:"); (()); JSONObject jsonObject = (user); (()+"\n"); (); (); } 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) { = name; } public String getPassword() { return password; } public void setPassword(String password) { = password; } }
The above tutorial on implementing simple socket communication for Java is all the content I have shared with you. I hope you can give you a reference and I hope you can support me more.