Java实现简单的socket通信教程
今天学习了一下java如何实现socket通信,感觉难点反而是在io上,因为java对socket封装已经很完善了。
今天代码花了整个晚上调试,主要原因是io的flush问题和命令行下如何运行具有package的类,不过最后问题基本都解决了,把代码贴出来供大家参考
server
publicclassTcpServer{ publicstaticvoidmain(String[]args)throwsException{ ServerSocketserver=newServerSocket(9091); try{ Socketclient=server.accept(); try{ BufferedReaderinput= newBufferedReader(newInputStreamReader(client.getInputStream())); booleanflag=true; intcount=1; while(flag){ System.out.println(客户端要开始发骚了,这是第+count+次!); count++; Stringline=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
publicclassTcpClient{ publicstaticvoidmain(String[]args)throwsException{ Socketclient=newSocket(127.0.0.1,9091); try{ PrintWriteroutput= newPrintWriter(client.getOutputStream(),true); Scannercin=newScanner(System.in); Stringwords; while(cin.hasNext()){ words=cin.nextLine(); output.println(words); System.out.println(写出了数据:+words); } cin.close(); }finally{ client.close(); } } }
补充知识:
服务端
importjava.io.BufferedReader; importjava.io.BufferedWriter; importjava.io.IOException; importjava.io.InputStreamReader; importjava.io.OutputStreamWriter; importjava.net.ServerSocket; importjava.net.Socket; importjava.util.Scanner; importnet.sf.json.JSONObject; publicclassServer_2{ publicstaticvoidmain(String[]args){ Scannerin=newScanner(System.in); InputStreamReaderisr; BufferedReaderbr; OutputStreamWriterosw; BufferedWriterrw; try{ ServerSocketserverSocket=newServerSocket(4444); Socketsocket=serverSocket.accept(); isr=newInputStreamReader(socket.getInputStream()); br=newBufferedReader(isr); Stringstr=br.readLine(); JSONObjectobject=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(IOExceptione){ e.printStackTrace(); } } }
客服端
importjava.io.BufferedReader; importjava.io.BufferedWriter; importjava.io.InputStreamReader; importjava.io.OutputStreamWriter; importjava.net.Socket; importjava.util.Scanner; importnet.sf.json.JSONObject; publicclassClient_2{ publicstaticvoidmain(String[]args){ Scannerin=newScanner(System.in); InputStreamReaderisr; BufferedReaderbr; OutputStreamWriterosw; BufferedWriterrw; try{ Socketsocket=newSocket("localhost",4444); osw=newOutputStreamWriter(socket.getOutputStream()); rw=newBufferedWriter(osw); Useruser=newUser(); 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()); JSONObjectjsonObject=JSONObject.fromObject(user); rw.write(jsonObject.toString()+"\n"); rw.close(); socket.close(); }catch(Exceptione){ //TODO:handleexception } } }
User
publicclassUser{ intID; Stringname; Stringpassword; publicintgetID(){ returnID; } publicvoidsetID(intiD){ ID=iD; } publicStringgetName(){ returnname; } publicvoidsetName(Stringname){ this.name=name; } publicStringgetPassword(){ returnpassword; } publicvoidsetPassword(Stringpassword){ this.password=password; } }
以上这篇Java实现简单的socket通信教程就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持毛票票。