当前位置: 技术问答>java相关
一个clientsocket的麻烦问题
来源: 互联网 发布时间:2015-02-09
本文导语: 以下是这个clientsocket代码,我想让它进入页面的时候自动连接上server,然后马上发送一个pathName,但是却怎么也连接不上,请高手帮忙看看是怎么回事,难道在start()中不能进行连接操作吗?(服务器端程序绝对没问题,已经经过...
以下是这个clientsocket代码,我想让它进入页面的时候自动连接上server,然后马上发送一个pathName,但是却怎么也连接不上,请高手帮忙看看是怎么回事,难道在start()中不能进行连接操作吗?(服务器端程序绝对没问题,已经经过测试)
import java.io.*;
import java.net.*;
import java.awt.*;
import java.applet.*;
public class browserSocket extends Applet implements Runnable
{
PrintStream os;
DataInputStream is;
Socket kkSocket=null;
Thread kicker=null;
String hostName="202.96.250.70";
String pathName="No";
public void init()
{
//String pathName=getParameter("pathName");
}
public void start()
{
if (connectHost(hostName))
{
sendData(pathName);
kicker=new Thread(this);
kicker.start();
}
}
public void run()
{
String fromWebServer=null;
while((fromWebServer=getData())!=null)
{
if (fromWebServer.equals("Yes"))
{
try
{
getAppletContext().showDocument(new URL(getCodeBase()+"main.jsp"));
}
catch(MalformedURLException e){}
}
else if (fromWebServer.equals("No"))
{
try
{
getAppletContext().showDocument(new URL(getCodeBase()+"errorpage.htm"));
}
catch(MalformedURLException e){}
}
}
}
public void stop()
{
if(kicker!=null) kicker.stop();
if(kicker!=null)
{
try
{
os.close();
is.close();
kkSocket.close();
kkSocket=null;
}
catch(Exception e){}
}
}
public boolean connectHost(String hostName)
{
try
{
kkSocket=new Socket(hostName,4700);
os=new PrintStream(kkSocket.getOutputStream());
is=new DataInputStream(kkSocket.getInputStream());
}
catch(UnknownHostException e)
{
System.out.println("Trying to connect to unknown host:"+e);
return false;
}
catch(Exception e)
{
System.out.println("Exception:"+e);
return false;
}
return true;
}
boolean sendData(String pathName)
{
try
{
os.println(pathName);
os.flush();
}
catch(Exception e)
{
return false;
}
return true;
}
String getData()
{
String fromWebServer;
try
{
fromWebServer=is.readLine();
}
catch(Exception e)
{
return "socket fail";
}
return fromWebServer;
}
}
import java.io.*;
import java.net.*;
import java.awt.*;
import java.applet.*;
public class browserSocket extends Applet implements Runnable
{
PrintStream os;
DataInputStream is;
Socket kkSocket=null;
Thread kicker=null;
String hostName="202.96.250.70";
String pathName="No";
public void init()
{
//String pathName=getParameter("pathName");
}
public void start()
{
if (connectHost(hostName))
{
sendData(pathName);
kicker=new Thread(this);
kicker.start();
}
}
public void run()
{
String fromWebServer=null;
while((fromWebServer=getData())!=null)
{
if (fromWebServer.equals("Yes"))
{
try
{
getAppletContext().showDocument(new URL(getCodeBase()+"main.jsp"));
}
catch(MalformedURLException e){}
}
else if (fromWebServer.equals("No"))
{
try
{
getAppletContext().showDocument(new URL(getCodeBase()+"errorpage.htm"));
}
catch(MalformedURLException e){}
}
}
}
public void stop()
{
if(kicker!=null) kicker.stop();
if(kicker!=null)
{
try
{
os.close();
is.close();
kkSocket.close();
kkSocket=null;
}
catch(Exception e){}
}
}
public boolean connectHost(String hostName)
{
try
{
kkSocket=new Socket(hostName,4700);
os=new PrintStream(kkSocket.getOutputStream());
is=new DataInputStream(kkSocket.getInputStream());
}
catch(UnknownHostException e)
{
System.out.println("Trying to connect to unknown host:"+e);
return false;
}
catch(Exception e)
{
System.out.println("Exception:"+e);
return false;
}
return true;
}
boolean sendData(String pathName)
{
try
{
os.println(pathName);
os.flush();
}
catch(Exception e)
{
return false;
}
return true;
}
String getData()
{
String fromWebServer;
try
{
fromWebServer=is.readLine();
}
catch(Exception e)
{
return "socket fail";
}
return fromWebServer;
}
}
|
我也知道是UnknownHostException ,具体是什么错。用printStackTrace看一下。
您可能感兴趣的文章:
本站(WWW.)旨在分享和传播互联网科技相关的资讯和技术,将尽最大努力为读者提供更好的信息聚合和浏览方式。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。