package com.lovo.socket;
import java.io.IOException;
import java.net.*;
public class Server
{
public static void main(String[] args)
{
try {
Socket socket = new Socket("localhost",4100);
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
//OutputStream out = socket.getOutputStream();
//PrintWriter writer = new PrintWriter(new OutputStreamWriter(out));
//writer.write("hello.");
//writer.flush();
}
}
始终都有异常.
connection 拒绝.
好象是IOException.
但不知道问题出在哪.
----------------解决方案--------------------------------------------------------
没看到哪里有ServerSocket
----------------解决方案--------------------------------------------------------
首先要确定自己本机已经是一个服务器,就是访问localhost会有网页返回,其实次好像我那样做啊
package com.lovo.socket;
import java.io.IOException;
import java.net.*;
public class Server
{
public static void main(String[] args)
{
try {
Socket socket = new Socket("localhost",4100); //应该是 http://localhost 而我访问是外网
//所以是http://www.it.com.cn
} catch (UnknownHostException e) { // 那个端口4100应该是80或者8080吧~
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
//OutputStream out = socket.getOutputStream(); //如果想读取从socket返回的字符,应该是
// socket.getInputStream()啊,
//PrintWriter writer = new PrintWriter(new OutputStreamWriter(out));
//writer.write("hello.");
//writer.flush();
}
}
[此贴子已经被作者于2007-10-19 20:58:45编辑过]
----------------解决方案--------------------------------------------------------
还有啊,如果想从网站那里读取字符,好像是用 URL 类 通过 getConnection();来读取的。
。。。。。我都很久没有用过socket了,。。。。差点记不起来啊
----------------解决方案--------------------------------------------------------
谢谢.
那是不是要开tomcat啊?
----------------解决方案--------------------------------------------------------
带main的是不能在WEB服务器上运行的
----------------解决方案--------------------------------------------------------
import java.net.*;
/*public class UdpSend
{
public static void main (String args[]) throws Exception
{
DatagramSocket send=new DatagramSocket();
String str="xiaoxufu l love you!";
send.send(new DatagramPacket(str.getBytes(),str.length(),InetAddress.getByName("127.0.0.1"),3000));
send.close();
}
}*/
/*处理中文字符*/
public class UdpSend
{
public static void main (String args[]) throws Exception
{
DatagramSocket send=new DatagramSocket();
String str="xiaoxufu 我爱你!";
send.send(new DatagramPacket(str.getBytes(),str.getBytes().length,InetAddress.getByName("127.0.0.1"),3000));
send.close();
}
}
----------------解决方案--------------------------------------------------------