别人给我出了一道socket题, 对与我来说太难了,请求各位大侠指点! 用socket实现代理的样子, 当命令行输入:telnet localhost 888(port) 的时候返回在程序里设定域名的回应,
让命令行与域名连接的交互点是这个用socket写出来的程序,这个程序算是起到代理的作用 。
(如果在console里能打印出hex模式就更好了); 各位大侠看在我怎么喜欢java的份上就帮帮小弟!
让命令行与域名连接的交互点是这个用socket写出来的程序,这个程序算是起到代理的作用 。
(如果在console里能打印出hex模式就更好了); 各位大侠看在我怎么喜欢java的份上就帮帮小弟!
http://liliugen.iteye.com/blog/521606
代理: 我们平时在浏览器里打百度的时候都是 www.baidu.com 才拿回来百度返回的数据,我要的效果是在浏览器里输入localhost就能跳转到百度。大概就是这个意思! 但是调试不是用浏览器,是用命令行的telnet命令测试。url是不可以使用的要sokcet纯实现。
telnet \socket \ httpproxy 三种客户端实现 没有socket程序被telnet操作吗? 我的意思是不倒如telnet相关的jar不行吗?
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;/**
* socket代理
* @author Administrator
*
*/
public class SocketJoin
{
public static void main(String[] args) throws IOException
{
ServerSocket serverSocket = null;
// 客户端与socket服务器
Socket socket = null;
// socket服务器与网址
Socket socketURL = null;
try
{
serverSocket = new ServerSocket(88);
while (true)
{
try
{
//等待本地登录
socket = serverSocket.accept();
//连接入网
socketURL = new Socket("192.9.200.81", 23);
// 浏览器中回执的信息
new Client(socket, socketURL).start();
// 客户端发给浏览器的信息
new Server(socket, socketURL).start();
} catch (IOException ioe)
{
socket.close();
socketURL.close();
ioe.printStackTrace();
//当连接失败的时候从新连接
continue;
}
}
} finally
{
serverSocket.close();
}
}
}/**
* 客户端
*
* @author Administrator
*
*/
class Client extends Thread
{
// 客户端与socket服务器
Socket socket = null;
// socket服务器与网址
Socket socketURL = null;
// 客户端与socket服务器
DataOutputStream out = null;
DataInputStream in = null;
public Client(Socket socket, Socket socketURL)
{
this.socket = socket;
this.socketURL = socketURL;
}
@Override
public void run()
{
try
{
//浏览器回执信息
in = new DataInputStream(socketURL.getInputStream());
// 通过socket服务器转回给客户
out = new DataOutputStream(socket.getOutputStream());
int temp = 0;
byte[] bs = new byte[1024];
while ((temp = in.read(bs)) > 0)
{
System.err.println(temp);
// 返回给客户信息
out.write(bs, 0, temp);
out.flush();
}
} catch (IOException ioe)
{
try
{
socket.close();
socketURL.close();
} catch (IOException e)
{
e.printStackTrace();
}
ioe.printStackTrace();
}
}
}/**
* 服务器
*/
class Server extends Thread
{
//客户端与socket服务器
private Socket socket = null;
//socket服务器与网址
private Socket socketURL = null;
// socket服务器传递给浏览器
DataOutputStream os = null;
DataInputStream is = null;
public Server(Socket socket, Socket socketURL)
{
this.socket = socket;
this.socketURL = socketURL;
}
@Override
public void run()
{
try
{
//客户输入信息
is = new DataInputStream(socket.getInputStream());
//浏览器接收
os = new DataOutputStream(socketURL.getOutputStream());
int tempURL = 0;
byte[] bs = new byte[1024];
while ((tempURL = is.read(bs)) > 0)
{
System.err.println(tempURL);
// 返回浏览器
os.write(bs, 0, tempURL);
os.flush();
}
} catch (IOException ioe)
{
try
{
socket.close();
socketURL.close();
} catch (IOException e)
{
e.printStackTrace();
}
ioe.printStackTrace();
}
}
}