客户端往服务端定时发送消息
2016-07-29 12:29
513 查看
服务端接收消息:
package disanzhou;
import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;
public class SocketServer {
public static void main(String[] args) throws IOException {
ServerSocket s = new ServerSocket(1000);
Socket socket = s.accept();
InputStream is = socket.getInputStream();
byte[] buffer = new byte[100];
int length = 0;
while (-1 != (length = is.read(buffer, 0, buffer.length))) {
String str = new String(buffer, 0, length, "utf-8");
System.out.println(str);
}
is.close();
socket.close();
s.close();
}
}
-------------------------------------------------------------------------
客户端发送消息:
package disanzhou;
import java.io.IOException;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Date;
import java.util.Timer;
public class MainTimerTask {
public static void main(String[] args) throws Exception, IOException {
Timer timer = new Timer();
Socket socket = new Socket("127.0.0.1", 1000);
timer.scheduleAtFixedRate(new MyTimerTask(socket), new Date(), 2000);
}
}
-------------------------------------------------
package disanzhou;
import java.io.IOException;
import java.io.OutputStream;
import java.net.Socket;
import java.util.Date;
import java.util.TimerTask;
public class MyTimerTask extends TimerTask {
private OutputStream os;
public MyTimerTask(Socket socket) {
try {
os = socket.getOutputStream();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@Override
public void run() {
String date = new Date(System.currentTimeMillis()).toLocaleString();
try {
String message1 = new String(date);
os.write(message1.getBytes("utf-8"));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
os.flush();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// System.out.println(str);
}
}
package disanzhou;
import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;
public class SocketServer {
public static void main(String[] args) throws IOException {
ServerSocket s = new ServerSocket(1000);
Socket socket = s.accept();
InputStream is = socket.getInputStream();
byte[] buffer = new byte[100];
int length = 0;
while (-1 != (length = is.read(buffer, 0, buffer.length))) {
String str = new String(buffer, 0, length, "utf-8");
System.out.println(str);
}
is.close();
socket.close();
s.close();
}
}
-------------------------------------------------------------------------
客户端发送消息:
package disanzhou;
import java.io.IOException;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Date;
import java.util.Timer;
public class MainTimerTask {
public static void main(String[] args) throws Exception, IOException {
Timer timer = new Timer();
Socket socket = new Socket("127.0.0.1", 1000);
timer.scheduleAtFixedRate(new MyTimerTask(socket), new Date(), 2000);
}
}
-------------------------------------------------
package disanzhou;
import java.io.IOException;
import java.io.OutputStream;
import java.net.Socket;
import java.util.Date;
import java.util.TimerTask;
public class MyTimerTask extends TimerTask {
private OutputStream os;
public MyTimerTask(Socket socket) {
try {
os = socket.getOutputStream();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@Override
public void run() {
String date = new Date(System.currentTimeMillis()).toLocaleString();
try {
String message1 = new String(date);
os.write(message1.getBytes("utf-8"));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
os.flush();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// System.out.println(str);
}
}
相关文章推荐
- 训练计划
- tabbaritem设置红点大小、自定义宽高
- javaWeb开发细节总结(3)
- new 出的对象无法使用 @Autowired装配进来的属性,会报null
- java 动态绑定
- 特征选择--scikit-learn
- hdu 5769 Substring 后缀数组
- Android RatingBar 自定义样式
- 题目94 cigarettes
- 第三方登录获取头像和昵称
- centos下yum安装jdk
- php: __autoload 与 sql_autoload_register 自动加载区别
- 计算两篇文章相似度代码
- 【动态规划】合唱团(动态规划)(最长上升子数列)
- F5 负载均衡
- c#字符串分割 split()技巧
- 基于Retrofit2,okhttp3的数据缓存(cache)技术
- C#随机数字生成的一种方法
- malloc/calloc/realloc/free与new/delete的对比
- Codeforces Round #295 (Div. 2) B bfs