展开全部
package sockettest;
import java.io.*;
import java.net.*;
import java.lang.Thread;
public class Client extends Thread{
private char[] buffer=new char[2048];
private Socket socket=null;
private String localIP="";
private String farIP="";
private int localPort=8765;
private int farPort=0;
private InputStream inKeyboard=System.in;
private InputStreamReader readKeyboard=new InputStreamReader(inKeyboard);
private BufferedReader getKeyboard=new BufferedReader(readKeyboard); //得到键盘输入的数据
private InputStream in=null;
private FileInputStream inFile=null;
private InputStreamReader inRead=null;
private BufferedReader read=null;
private OutputStream out=null;
private FileOutputStream outFile=null;
private OutputStreamWriter outWrite=null;
private BufferedWriter write=null;
private int timeout=30000;
public void clearBuffer(){
for(int i=0;i
buffer[i]=0;
}
public Client()throws Exception {
socket=new Socket();
}
public Client(String ip,int port)throws Exception{
socket=new Socket(ip,port);
localIP=socket.getLocalAddress().getHostAddress();
localPort=socket.getLocalPort();
}
public void bind(String ip,int port)throws Exception{ //绑定工作
SocketAddress serverAddr=new InetSocketAddress(ip,port);
socket.bind(serverAddr);
localIP=socket.getLocalAddress().getHostAddress();
localPort=socket.getLocalPort();
}
public void setTimeout(int time)throws Exception{
timeout=time;
socket.setSoTimeout(timeout);
}
public void begin()throws Exception{ //开始输入输出的变更封装
in=socket.getInputStream();
inRead=new InputStreamReader(in);
read=new BufferedReader(inRead);
out=socket.getOutputStream();
outWrite=new OutputStreamWriter(out);
write=new BufferedWriter(outWrite);
}
public void writeFromKeyboard()throws Exception{ //使用62616964757a686964616fe58685e5aeb931333238653261键盘的输入写到套接字连接 :限制每次128个字节
String s="";
s=getKeyboard.readLine();
write.write(s);
write.flush();
}
public void writeFromFile(String filePath,long off,int len)throws Exception{ //将文件里的内容写到套接字连接,参数:文件,开始,长度
inFile=new FileInputStream(filePath);
InputStreamReader i=new InputStreamReader(inFile);
BufferedReader r=new BufferedReader(i);
r.skip(off);
char[] data=new char[len];
int num=r.read(data);
r.close();
write.write(data,0,num);
write.flush();
}
public String readToPrint()throws Exception{ //读取套接字连接里的内容,显示到屏幕 :限制1024个字节
clearBuffer();
int num=read.read(buffer);
String s=String.valueOf(buffer,0,num);
return s;
}
public void readToFile(String filePath,boolean on)throws Exception{ //将读取的数据写到套接字连接中 参数:文件名, true(添加)
outFile=new FileOutputStream(filePath,on);
OutputStreamWriter o=new OutputStreamWriter(outFile);
BufferedWriter w=new BufferedWriter(o);
clearBuffer();
int num=read.read(buffer);
String s=String.valueOf(buffer,0,num);
w.write(s);
w.flush();
}
public void closeLink()throws Exception{ //关闭当前套接字连接
socket.close();
}
public String getLocalIP()throws Exception{ //返回本地地址
return localIP;
}
public int getLocalPort()throws Exception{ //返回本地端口
return localPort;
}
public String getFarIP()throws Exception{ //返回远程主机地址
farIP=socket.getInetAddress().getHostAddress();
return farIP;
}
public int getFarPort()throws Exception{ //返回远程主机端口
farPort=socket.getPort();
return farPort;
}
public void setBufferSize(int size){
buffer=new char[size];
}
}
---------------------------------------------------
package sockettest;
import java.io.*;
import java.net.*;
import java.lang.Thread;
public class Server extends Thread{
private char[] buffer=new char[2048];
private ServerSocket server=null;
private Socket socket=null;
private String localIP="";
private String farIP="";
private int localPort=8765;
private int farPort=0;
private InputStream inKeyboard=System.in;
private InputStreamReader readKeyboard=new InputStreamReader(inKeyboard);
private BufferedReader getKeyboard=new BufferedReader(readKeyboard); //得到键盘输入的数据
private InputStream in=null;
private FileInputStream inFile=null;
private InputStreamReader inRead=null;
private BufferedReader read=null;
private OutputStream out=null;
private FileOutputStream outFile=null;
private OutputStreamWriter outWrite=null;
private BufferedWriter write=null;
private int timeout=30000;
public void clearBuffer(){
for(int i=0;i
buffer[i]=0;
}
public Server(String ip,int port) throws Exception{ //参数:服务器地址 端口
SocketAddress serverAddr=new InetSocketAddress(ip,port);
server=new ServerSocket();
server.bind(serverAddr);
server.setSoTimeout(timeout);
}
public Server()throws Exception{
SocketAddress serverAddr=new InetSocketAddress("127.0.0.1",8765);
server=new ServerSocket();
server.bind(serverAddr);
server.setSoTimeout(timeout);
}
public void bind(String ip,int port)throws Exception{ //绑定工作
SocketAddress serverAddr=new InetSocketAddress(ip,port);
server.bind(serverAddr);
}
public void accept()throws Exception{ //开始侦听端口
socket= server.accept();
localIP=socket.getLocalAddress().getHostAddress();
localPort=socket.getLocalPort();
}
public void setTimeout(int time)throws Exception{
timeout=time;
server.setSoTimeout(timeout);
}
public void begin()throws Exception{ //开始输入输出的变更封装
in=socket.getInputStream();
inRead=new InputStreamReader(in);
read=new BufferedReader(inRead);
out=socket.getOutputStream();
outWrite=new OutputStreamWriter(out);
write=new BufferedWriter(outWrite);
}
public void writeFromKeyboard()throws Exception{ //使用键盘的输入写到套接字连接 :限制每次128个字节
String s="";
s=getKeyboard.readLine();
write.write(s);
write.flush();
}
public void writeFromFile(String filePath,long off,int len)throws Exception{ //将文件里的内容写到套接字连接,参数:文件,开始,长度
inFile=new FileInputStream(filePath);
InputStreamReader i=new InputStreamReader(inFile);
BufferedReader r=new BufferedReader(i);
r.skip(off);
char[] data=new char[len];
int num=r.read(data);
r.close();
write.write(data,0,num);
write.flush();
}
public String readToPrint()throws Exception{ //读取套接字连接里的内容,显示到屏幕 :限制1024个字节
clearBuffer();
int num=read.read(buffer);
String s=String.valueOf(buffer,0,num);
return s;
}
public void readToFile(String filePath,boolean on)throws Exception{ //将读取的数据写到套接字连接中 参数:文件名, true(添加)
outFile=new FileOutputStream(filePath,on);
OutputStreamWriter o=new OutputStreamWriter(outFile);
BufferedWriter w=new BufferedWriter(o);
clearBuffer();
int num=read.read(buffer);
String s=String.valueOf(buffer,0,num);
w.write(s);
w.flush();
}
public void closeLink()throws Exception{ //关闭当前套接字连接
socket.close();
}
public void closeServer()throws Exception{ //关闭套接字服务器
server.close();
}
public String getLocalIP()throws Exception{ //返回本地地址
return localIP;
}
public int getLocalPort()throws Exception{ //返回本地端口
return localPort;
}
public String getFarIP()throws Exception{ //返回远程主机地址
farIP=socket.getInetAddress().getHostAddress();
return farIP;
}
public int getFarPort()throws Exception{ //返回远程主机端口
farPort=socket.getPort();
return farPort;
}
public void setBufferSize(int size){
buffer=new char[size];
}
}
本回答被提问者采纳
已赞过
已踩过<
你对这个回答的评价是?
评论
收起