通信小结
寒假在蓝杰呆了有半个月,年前一周,年后一周。算是寒假集训吧,学习了关于通信方面的一些内容。
总结的说,通信可以分为两个部分,一是服务器,一是客户端。
一、服务器
总结来说两句代码:
//创建一个服务器ServerSocket server = new ServerSocket (服务器端口);//在等待客户机连结进入,进入后,生成一个Socket对象Socket client = server.accept();
然后从Socket对象上得到相应的输入输出流即可。
二、客户端
只需要一句代码:
//创建一个客户端Socket client=new Socket (“服务器IP”,服务器端口);
同样从Socket对象上得到相应的输入输出流。
若要进行信息传输,则加上相应的传输和处理消息的方法即可。可应用InputStream和OutputStream的write() 和read() 方法,将消息转换成Byte型的,再进行传输。
下面是一个简单的传送消息的程序。
首先是服务器端的代码:
public void setServer(int port){try{ServerSocket server = new ServerSocket(port);System.out.println("服务器创建成功!"+port);while(true){Socket client = server.accept();//创建一个线程对象传入时进入的连结st = new ServerThread(client,jtf);st.start();System.out.println("已经启动了一个线程去处理对象了。");}}catch(Exception e){e.printStackTrace();}}服务器发消息时有开一个线程,所以定义一个线程类:
public class ServerThread extends Thread{private Socket client;private OutputStream out;//输出流对象public JTextField text;public String msg;public ServerThread(Socket sc,JTextField text){client = sc;this.text = text;}//将发送消息的代码包装到一个方法中public void sendMsg(String msg)throws Exception{byte[] data = msg.getBytes();//发送对象,并强制输出out.write(data);out.flush();}//重写run方法public void run(){processMsg(client);//执行方法后,线程自己退出。}//处理连接对象public void processMsg(Socket client){try{out = client.getOutputStream();InputStream ins = client.getInputStream();String s = "welcome to 服务器\r\n";this.sendMsg(s);String ints = readString(ins);while (!ints.equals("bye")){//System.out.println("客户机说:"+ints);System.out.println(s);msg = "服务器说:"+text.getText()+"\r\n";this.sendMsg(msg);//发送字符串给客户机对象//再次获取字符串字节ints = readString(ins);}s = "come back again!!!\n";this.sendMsg(s);client.close();//关闭客户端}catch(Exception ef){ef.printStackTrace();}}/** * 从输入流中读取字节,组成字符串返回 * 字节=13时,表明之前的是一个字符串 * @param ins * @return 从流上读到的字符串 */public String readString(InputStream ins) throws Exception{//创建一个字符串的缓冲区StringBuffer stb = new StringBuffer();char c = 0;while (c!=13){int i = ins.read();//读取客户机发来的字节c = (char)i;stb.append(c);}//将读到的字节转换成字符串String ints = stb.toString().trim();return ints;}}然后是客户端的代码,从客户端中输入消息:
public class Sampleclient extends Thread{private String IP;//客户机的IPprivate int port;//客户机的端口private OutputStream ous;//输出到服务器的输出流private BufferedReader br;//输入到服务器的输入流//创建客户机对象时,需要传入服务器的IP和端口public Sampleclient(String IP,int port) {this.IP = IP;this.port = port;}//首先,连结服务器public void connectServer(){try {//创建一个到服务器端的Socket对象Socket client = new Socket (this.IP,this.port);InputStream ins = client.getInputStream();ous = client.getOutputStream();//继续读取字符串,很多遍了。br = new BufferedReader(new InputStreamReader(ins));String input = br.readLine();System.out.println("服务器说: "+input);ous.flush();}catch (Exception e) {e.printStackTrace();}}//重写run方法,线程中读取服务器发来的消息public void run(){while(true){readFromServer();}} //从线程中读取消息,这个方法会阻塞,必须在独立线程中public void readFromServer(){try {String input = br.readLine();System.out.println("服务器说: "+input);} catch (Exception e) {e.printStackTrace();}}//发送一条消息到服务器的方法public void sendMsg(String msg){try{msg+="\r\n";this.ous.write(msg.getBytes());this.ous.flush();}catch(Exception ef){ef.printStackTrace();}}//程序启动主函数public static void main(String[] args) {Sampleclient ct = new Sampleclient("localhost",8080);ct.connectServer();//连结服务器成功//创建一个从命令行读取用户输入的扫描器对象Scanner sc=new Scanner(System.in);ct.start();//启动读取线程while(true){//继续读取命令行输入发给服务器System.out.println("输入要发送的消息:");String msg=sc.next();System.out.println("客户端说: "+msg);ct.sendMsg(msg);}}}这段代码运行时还有一些小瑕疵,各位看了不要直接Copy,要稍作修改。