寻求简单通讯的例子 要求tcp/ip同步短连接即: 客户端发送信息时连接,得到服务响应(信息的返回,问题的回答等等)客户端中止连接。再发送时重新连接。-----------------告急-------------- 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 服务端:import java.io.DataInputStream;import java.io.DataOutputStream;import java.io.IOException;import java.net.ServerSocket;import java.net.Socket;public class SimpleTestServer { public static void main(String[] args) { try { ServerSocket ssocket = new ServerSocket(2000); //port while (true) { Socket socket = ssocket.accept(); socket.setSoTimeout(0); DataInputStream dataIn = new DataInputStream(socket .getInputStream()); byte[] readBytes = new byte[1000]; int length = dataIn.read(readBytes); if (length == -1) { break; } /** * 处理请求,比如打印收到的信息。 */ System.out.println(new String(readBytes,0,length)); /** * 构造应答信息 */ byte[] reply = new byte[] { 'r', 'e', 'p', 'l', 'y' }; DataOutputStream dataOut = new DataOutputStream(socket .getOutputStream()); /** * 发送应答信息 */ dataOut.write(reply); dataOut.flush(); dataOut.close(); /** * 关闭连接 */ socket.close(); } } catch (IOException e) { e.printStackTrace(); } }}客户端:import java.io.DataInputStream;import java.io.DataOutputStream;import java.io.IOException;import java.net.Socket;public class SimpleTestClient { public static void main(String[] args) { try { Socket socket = new Socket("127.0.0.1",2000); //ip port socket.setSoTimeout(0); DataOutputStream dataOut = new DataOutputStream(socket .getOutputStream()); byte[] requestBytes=new byte[]{'r','e','q','u','e','s','t'}; dataOut.write(requestBytes); dataOut.flush(); DataInputStream dataIn = new DataInputStream(socket .getInputStream()); byte[] readBytes = new byte[1000]; int length = dataIn.read(readBytes); dataIn.close(); if(length==-1) { dataOut.close(); socket.close(); } System.out.println(new String(readBytes,0,length)); dataOut.close(); socket.close(); }catch(IOException e) { e.printStackTrace(); } }} 求助:java作业题 如何定时终止/开启一个java程序? 关于weblogic日志的问题,急!!! java陣列的應用~想不出來了.. Linux下的JAVA支持? 给小弟解释一哈~!刚学java log4j 如何通过配置文件实现日志信息入库,不写代码只是写配置文件????? 为什么 compareTo可以直接用,而compare不能? 为什么会出现java.lang.InterruptedException? 在jbuilder7能运行applet怎么让他单独在IE上运行呢? 记事本的新建菜单事件怎么写 itext打印汉字的问题,谢谢
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;public class SimpleTestServer {
public static void main(String[] args) {
try {
ServerSocket ssocket = new ServerSocket(2000);
//port
while (true) {
Socket socket = ssocket.accept();
socket.setSoTimeout(0);
DataInputStream dataIn = new DataInputStream(socket
.getInputStream());
byte[] readBytes = new byte[1000];
int length = dataIn.read(readBytes);
if (length == -1) {
break;
}
/**
* 处理请求,比如打印收到的信息。
*/
System.out.println(new String(readBytes,0,length));
/**
* 构造应答信息
*/
byte[] reply = new byte[] { 'r', 'e', 'p', 'l', 'y' };
DataOutputStream dataOut = new DataOutputStream(socket
.getOutputStream());
/**
* 发送应答信息
*/
dataOut.write(reply);
dataOut.flush();
dataOut.close();
/**
* 关闭连接
*/
socket.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}客户端:
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;public class SimpleTestClient {
public static void main(String[] args) {
try
{
Socket socket = new Socket("127.0.0.1",2000);
//ip port
socket.setSoTimeout(0);
DataOutputStream dataOut = new DataOutputStream(socket
.getOutputStream());
byte[] requestBytes=new byte[]{'r','e','q','u','e','s','t'};
dataOut.write(requestBytes);
dataOut.flush();
DataInputStream dataIn = new DataInputStream(socket
.getInputStream());
byte[] readBytes = new byte[1000];
int length = dataIn.read(readBytes);
dataIn.close();
if(length==-1)
{
dataOut.close();
socket.close();
}
System.out.println(new String(readBytes,0,length));
dataOut.close();
socket.close();
}catch(IOException e)
{
e.printStackTrace();
}
}
}