java服务器端程序报错,怎样修改?
第一处错误:
Systemoutprintln(“INFO:。。。。“ + thisport=" + ",operateStr + "" + thisoperateStr +""");// 错误,检查你的引号
改成
Systemoutprintln(“INFO:。。。。“ + thisport=" + ",operateStr + " + thisoperateStr + "");
第二处错误:
String[] dps = ipsplit("\"); // 需对\转义
改成
String[] dps = ipsplit("\\");
public class ServerConnect {
private static final int BUF_SIZE = 1024;
private static final int PORT = 8080;
private static final int TIMEOUT = 3000;
public static void main(String[] args) {
selector();
}
public static void handleAccept(SelectionKey key) throws IOException {
ServerSocketChannel ssChannel = (ServerSocketChannel) keychannel();
SocketChannel sc = ssChannelaccept();
scconfigureBlocking(false);
scregister(keyselector(), SelectionKeyOP_READ,
ByteBufferallocateDirect(BUF_SIZE));
}
public static void handleRead(SelectionKey key) throws IOException {
SocketChannel sc = (SocketChannel) keychannel();
ByteBuffer buf = (ByteBuffer) keyattachment();
long bytesRead = scread(buf);
while (bytesRead > 0) {
bufflip();
while (bufhasRemaining()) {
byte b= bufget();
Systemoutprint((char) bufget());
}
Systemoutprintln();
bufclear();
bytesRead = scread(buf);
}
if (bytesRead == -1) {
scclose();
}
}
public static void handleWrite(SelectionKey key) throws IOException {
ByteBuffer buf = (ByteBuffer) keyattachment();
bufflip();
SocketChannel sc = (SocketChannel) keychannel();
while (bufhasRemaining()) {
scwrite(buf);
}
bufcompact();
}
public static void selector() {
Selector selector = null;
ServerSocketChannel ssc = null;
try {
selector = Selectoropen();
ssc = ServerSocketChannelopen();
sscsocket()bind(new InetSocketAddress(PORT));
sscconfigureBlocking(false);
sscregister(selector, SelectionKeyOP_ACCEPT);
while (true) {
if (selectorselect(TIMEOUT) == 0) {
Systemoutprintln("==");
continue;
}
Iterator<SelectionKey> iter = selectorselectedKeys()
iterator();
int count = 0;
while (iterhasNext()) {
count++;
SelectionKey key = iternext();
if (keyisAcceptable()) {
handleAccept(key);
}
if (keyisReadable()) {
handleRead(key);
}
if (keyisWritable() && keyisValid()) {
handleWrite(key);
}
if (keyisConnectable()) {
Systemoutprintln("isConnectable = true");
}
iterremove();
}
Systemoutprintln(count);
}
} catch (IOException e) {
eprintStackTrace();
} finally {
try {
if (selector != null) {
selectorclose();
}
if (ssc != null) {
sscclose();
}
} catch (IOException e) {
eprintStackTrace();
}
}
}
}
不知道客户端的请求是什么形式的请求socketwebservicehttp
你这个这是用nio写的一个socket服务端程序,监听的是本地8080端口,在handleWrite方法体类处理jdbc获取数据放入ByteBuffer里就可以了,jdbc操作这里就不写了。
你的问题应该是:在java中通过编程设置代理服务器并访问网络,现在作答如下:
1,讲解一下基础知识:
HTTP:是应用层协议,是基于传输层协议的。
TCP: 是传输层协议,是基于网络层协议的。
IP: 是网络层协议。
一个TCP的连接要进行三次握手(就像转户口一样,不详说),HTTP只是一个应用协议,也就是相当于一个自定义协议,即其没有对底层的传输方式进行干涉,只是对数据内容格式进行了定义。
2,
我们再说说HTTP代理,从上可以理解,HTTP代理服务器就是这样一台机器:你把所有的HTTP请求都发到这个
HTTP代理服务器,然后这个HTTP代理服务器请求你要访问的最终地址,把响应回传给你。这里还要注意它代理的是HTTP协议,而HTTP又是基于
TCP的,也就是说这个服务器代理的是指定HTTP内容格式的TCP连接。再说下去也没意思了,看以下代码:
//以下地址是代理服务器的地址
Socket socket = new Socket("1012188", 80);
//写与的内容就是遵循HTTP请求协议格式的内容,请求百度
socketgetOutputStream()write(new String("GET http://wwwbaiducom/ HTTP/11\r\n\r\n")getBytes());
byte[] bs = new byte[1024];
InputStream is = socketgetInputStream();
int i;
while ((i = isread(bs)) > 0) {
Systemoutprintln(new String(bs, 0, i));
}
isclose();
3,当然在Java中,有Proxy代理上网的使用,此时使用URL(HTTP)就不涉及Socket(TCP)了,看如下代码
//设置代理
SystemsetProperty("httpproxySet", "true");
SystemsetProperty("httpproxyHost", "1012188");
SystemsetProperty("httpproxyPort", "80");
//直接访问目的地址
URL url = new URL("http://wwwbaiducom");
URLConnection con = urlopenConnection();
InputStreamReader isr = new InputStreamReader(congetInputStream());
char[] cs = new char[1024];
int i = 0;
while ((i = isrread(cs)) > 0) {
Systemoutprintln(new String(cs, 0, i));
}
isrclose();
0条评论