日期:2014-05-20 浏览次数:20791 次
package s;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.Socket;
import java.net.UnknownHostException;
public class C_port {
Socket s = null;
OutputStream 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();
}
String str = "CJ必胜!!CJCO!!CJHMF,这个问题解决了,我高兴了,今天我建群,加入了130个人\r\n我们群里的人,都决心互相学习,增长本领";
byte[] b = str.getBytes();
int len = b.length;
try {
dops.write(b,0,len);
dops.close();
s.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void main(String[] args){
new C_port();
}
}
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();
}
}
SystemThread m1 = new SystemThread(so);
m1.start();
try {
s.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void main(String[] args){
new S_port();
}
}
package s;
import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.Socket;
public class SystemThread extends Thread{
Socket so = null;
DataInputStream dips = null;
InputStream inputstream = null;
String str = null;
public SystemThread(Socket s){
so = s;
}
public void run(){
byte[] buf = new byte[2048*512];
int num = 0;
try {
inputstream = so.getInputStream();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
while (num != (-1)) {//是否读完所有数据
try {
num = inputstream.read(buf);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}//继续从网络中读取文件
}
str = new String(buf);
System.out.println(str);
}
}