博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
网络client和server交互
阅读量:5252 次
发布时间:2019-06-14

本文共 1364 字,大约阅读时间需要 4 分钟。

package test;

import java.io.BufferedReader;

import java.io.IOException;
import java.io.InputStreamReader;
import java.net.Socket;
import java.net.UnknownHostException;

public class DailyAdviceClient {

public void go(){
try {
Socket s = new Socket("127.0.0.1",5000);
InputStreamReader ir = new InputStreamReader(s.getInputStream());
BufferedReader br = new BufferedReader(ir);
String Advice = br.readLine();
System.out.println(Advice);
br.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void main(String[] args) {
DailyAdviceClient d = new DailyAdviceClient();
d.go();
}
}

 

package test;

import java.io.IOException;

import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.rmi.server.SocketSecurityException;

public class DailyAdviceServer {

static String[] words = {"aaaaa","bbbbb","ccccc"};

public static void go(){
try {
ServerSocket ss = new ServerSocket(5000);
while(true){
Socket s = ss.accept();
PrintWriter pw = new PrintWriter(s.getOutputStream());
String Advice = getAdvice();
pw.println(Advice);
pw.close();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

private static String getAdvice() {

int random =(int)( Math.random() * words.length);
return words[random];
}
}

 

转载于:https://www.cnblogs.com/hy168/p/5845288.html

你可能感兴趣的文章
Jmeter接口压力测试,Java.net.BindException: Address already in use: connect
查看>>
ASP.NET使网页弹出窗口不再困难
查看>>
Leetcode Balanced Binary Tree
查看>>
Leetcode 92. Reverse Linked List II
查看>>
windown快速安装xgboost
查看>>
Linux上安装Libssh2
查看>>
九.python面向对象(双下方法内置方法)
查看>>
go:channel(未完)
查看>>
[JS]递归对象或数组
查看>>
LeetCode(17) - Letter Combinations of a Phone Number
查看>>
Linux查找命令对比(find、locate、whereis、which、type、grep)
查看>>
路由器外接硬盘做nas可行吗?
查看>>
python:从迭代器,到生成器,再到协程的示例代码
查看>>
Java多线程系列——原子类的实现(CAS算法)
查看>>
在Ubuntu下配置Apache多域名服务器
查看>>
多线程《三》进程与线程的区别
查看>>
linux sed命令
查看>>
html标签的嵌套规则
查看>>
[Source] Machine Learning Gathering/Surveys
查看>>
HTML <select> 标签
查看>>