import java.net.*;
import java.io.*;/**
*
* @author Administrator
*/
public class Main extends Thread{
@Override
public void run(){
ServerSocket s = null;
Socket s1;
String sendString = "Hello Net World!";
OutputStream s1out;
DataOutputStream dos;
try{
s=new ServerSocket(5432);
} catch (IOException e) { }
// Run the listen/accept loop forever
while (true) {
try {
// Wait here and listen for a connection
s1=s.accept();
// Get a communication stream for soocket
s1out = s1.getOutputStream();
dos = new DataOutputStream (s1out);
// Send your string! (UTF provides machine-independent format)
dos.writeUTF(sendString);
// Close the connection, but not the server socket
s1out.close();
s1.close();
} catch (IOException e) { }
} //end of while
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// Register your service on port 5432
Thread t=new Thread();
t.start();// TODO code application logic here
}}求运行结果?急!!!
import java.io.*;/**
*
* @author Administrator
*/
public class Main extends Thread{
@Override
public void run(){
ServerSocket s = null;
Socket s1;
String sendString = "Hello Net World!";
OutputStream s1out;
DataOutputStream dos;
try{
s=new ServerSocket(5432);
} catch (IOException e) { }
// Run the listen/accept loop forever
while (true) {
try {
// Wait here and listen for a connection
s1=s.accept();
// Get a communication stream for soocket
s1out = s1.getOutputStream();
dos = new DataOutputStream (s1out);
// Send your string! (UTF provides machine-independent format)
dos.writeUTF(sendString);
// Close the connection, but not the server socket
s1out.close();
s1.close();
} catch (IOException e) { }
} //end of while
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// Register your service on port 5432
Thread t=new Thread();
t.start();// TODO code application logic here
}}求运行结果?急!!!
Thread t=new Thread();
这是创建一个默认的线程,效用它的run方法应该什么都没有,不会走你上面写的run方法,你可以这样写:
Main main = new Main();
main.start();这样应该走你自定义的线程了
这个程序只是socket服务端 等待客户端socket的连接 如果没有,自然不会看到任何结果
还有如4楼所说 初始化类对象找错了加上客户端程序,并一起初始化一下class Client {
Socket sc;
InputStream sin;
DataInputStream dis;
public Client() {
while (sc == null) {
try {
sc = new Socket("localhost", 5432);
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
try {
sin = sc.getInputStream();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
dis = new DataInputStream(sin);
String input = null;
try {
input = dis.readUTF();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(input);
try {
sin.close();
dis.close();
sc.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}good luck
可以使用telnet之类的程序,连接一下,应该会有结果。
但是,还要注意编码问题。
能具体点吗
注意看你的main函数//原始
Thread t = new Thread();//启动的是一个空线程。所以没有结果
t.start();
//修改后
Thread t = new Thread(new Main());//启动的是自己手动写的一个线程。根据你的代码判断,该线程会作为server运行,等待其他client访问
t.start();