日期:2014-05-20 浏览次数:20913 次
package s;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
public class S_port {
String str = "";
ServerSocket s =null;
Socket so = null;
S_port(){
try {
s = new ServerSocket(60000);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
while(true){
try {
so = s.accept();
break;
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
str = Lib.readsocketUTF(so);
System.out.println(str);
}
public static void main(String[] args){
new S_port();
}
}
package s;
import java.io.DataInputStream;
import java.io.EOFException;
import java.io.IOException;
import java.net.Socket;
public class Lib {
static String readsocketUTF(Socket s){
DataInputStream read = null;
try {
read = new DataInputStream(s.getInputStream());
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
String msg = "";
String info = "";
msg = info;
while(true){
try {
info = read.readUTF();
}catch(EOFException e){
break;
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
msg = msg + info;
return msg;
}
}
package s;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
import java.net.UnknownHostException;
public class C_port {
Socket s = null;
DataOutputStream dops = null;
public C_port(){
try {
s = new Socket("127.0.0.1",60000);
dops = new DataOutputStream(s.getOutputStream());
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
dops.writeUTF("Hello_world");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void main(String[] args){
new C_port();
}
}