当前位置: 技术问答>java相关
请教高手applet如何用socket和servlet进行通讯?(最好有项细代码)
来源: 互联网 发布时间:2017-03-09
本文导语: rt | 下面是用socket和servlet进行通讯的客户端代码,要注意,JAVA的权限要求,在APPLET中进行socket通讯,servlet和APPLET的WEB服务器必须在一个IP地址上 con = url.openConnection(); //设置连...
rt
|
下面是用socket和servlet进行通讯的客户端代码,要注意,JAVA的权限要求,在APPLET中进行socket通讯,servlet和APPLET的WEB服务器必须在一个IP地址上
con = url.openConnection();
//设置连接属性
con.setDoInput(true);
con.setDoOutput(true);
con.setUseCaches(false);
//取得输出流
writer = new PrintWriter(con.getOutputStream());
//输出对象
writer.println(argInput);
writer.flush();
writer.close();
//取得输入流
reader = new BufferedReader(new InputStreamReader(con.getInputStream()));
String buffer = reader.readLine();
con = url.openConnection();
//设置连接属性
con.setDoInput(true);
con.setDoOutput(true);
con.setUseCaches(false);
//取得输出流
writer = new PrintWriter(con.getOutputStream());
//输出对象
writer.println(argInput);
writer.flush();
writer.close();
//取得输入流
reader = new BufferedReader(new InputStreamReader(con.getInputStream()));
String buffer = reader.readLine();
|
需要修改一个文件,路径是: “你的jdk的目录“jrelibsecurityjava.policy。
在文件中加入代码:
permission java.net.SocketPermission "-", "listen";
permission java.net.SocketPermission "-", "accept";
permission java.net.SocketPermission "-", "connect";
permission java.net.SocketPermission "-", "read";
permission java.net.SocketPermission "-", "write";
在文件中加入代码:
permission java.net.SocketPermission "-", "listen";
permission java.net.SocketPermission "-", "accept";
permission java.net.SocketPermission "-", "connect";
permission java.net.SocketPermission "-", "read";
permission java.net.SocketPermission "-", "write";
|
ServerSocket serverSocket = null;
private DataOutputStream outStream = null;
private DataInputStream inStream = null;
try {
serverSocket = new ServerSocket(port);
socket = serverSocket.accept();
//initialize socket,outStream
inStream = new DataInputStream(new BufferedInputStream(socket.getInputStream()));
outStream = new DataOutputStream(new BufferedOutputStream(socket.getOutputStream()));
//receive Msg from client
int len = inStream.readInt();
//if(len 0) {
int k=inStream.read(readBytes,i,j);
i=i+k;
j=j-k;
}
String clientMsg = new String(readBytes,0,len,"GB2312");
//send Ack to client
byte[] sendBytes = null;
sendBytes = "error".getBytes("GB2312");
outStream.writeInt(sendBytes.length);
outStream.write(sendBytes);
outStream.flush();
outStream.close();
socket.close();
}
catch(Exception e) {
if(socket != null) {
socket.close();
}
if(outStream != null) {
outStream.close();
}
if(inStream != null) {
inStream.close();
}
}
private DataOutputStream outStream = null;
private DataInputStream inStream = null;
try {
serverSocket = new ServerSocket(port);
socket = serverSocket.accept();
//initialize socket,outStream
inStream = new DataInputStream(new BufferedInputStream(socket.getInputStream()));
outStream = new DataOutputStream(new BufferedOutputStream(socket.getOutputStream()));
//receive Msg from client
int len = inStream.readInt();
//if(len 0) {
int k=inStream.read(readBytes,i,j);
i=i+k;
j=j-k;
}
String clientMsg = new String(readBytes,0,len,"GB2312");
//send Ack to client
byte[] sendBytes = null;
sendBytes = "error".getBytes("GB2312");
outStream.writeInt(sendBytes.length);
outStream.write(sendBytes);
outStream.flush();
outStream.close();
socket.close();
}
catch(Exception e) {
if(socket != null) {
socket.close();
}
if(outStream != null) {
outStream.close();
}
if(inStream != null) {
inStream.close();
}
}
|
Socket s = new Socket(hostIP,hostPort);