由于网络上的socket举例在本人看来并不是最简单的socket举例,所以我给出以下举例。
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;
public class Server {
public void main(String[] args){
try {
ServerSocket serverSocket=new ServerSocket(4700);
Socket socket=serverSocket.accept();
InputStream is=socket.getInputStream();
byte[] b=new byte[1024];
is.read(b);
String getStr=new String(b);
System.out.println(getStr);
}catch (Exception e){
e.printStackTrace();
}
}
}
import java.io.OutputStream;
import java.net.Socket;
import java.util.Scanner;
public class Client {
public static void main(String[] args){
try {
Socket socket=new Socket("192.168.0.6",4700);
Scanner scan=new Scanner(System.in);
OutputStream os=socket.getOutputStream();
os.write(scan.nextLine().getBytes("utf-8"));//由于我使用的是centos作为服务端,其编码格式为utf-8,为了避免乱码,所以使用这个编码格式转换成字节流
os.close();
scan.close();
socket.close();
} catch(Exception e){
e.printStackTrace();
}
}
}
socket网络上传单个文件
public class Client1 {
public Socket socket;
public Client1(){
try {
socket=new Socket("192.168.0.6",4700);
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public Client1(String intelAddr,int port){
try {
socket=new Socket(intelAddr,port);
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public boolean sendFile(String fileName){
OutputStream os=null;
try {
os=socket.getOutputStream();
boolean bl1=sendFileName(fileName,os);
boolean bl2=sendFile(fileName,os);
return bl1&&bl2;
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
return false;
}finally{
try {
os.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public boolean sendFileName(String fileName,OutputStream os){
String[] fileNames=fileName.split("[\\/]");
fileName=fileNames[fileNames.length-1];
try {
os.write(fileName.getBytes("utf-8"));
os.flush();
return true;
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return false;
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return false;
}
}
public boolean sendFile(String fileName,OutputStream os){
File f=new File(fileName);
try {
FileInputStream fis=new FileInputStream(f);
byte[] b=new byte[1024];
int len=0;
while((len=fis.read(b))>0){
os.write(b, 0, len);
}
os.flush();
fis.close();
return true;
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return false;
}catch(Exception e){
e.printStackTrace();
return false;
}
}
}
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;
public class Server {
public static void main(String[] args){
try {
ServerSocket ss=new ServerSocket(4700);
Socket socket=ss.accept();
InputStream is=socket.getInputStream();
byte[] b=new byte[1024];
int len=0;
is.read(b);
String fileName=new String(b);
File newFile=new File(fileName);
newFile.mkdirs();
FileOutputStream fos=new FileOutputStream("/"+newFile);
while((len=is.read(b))>0){
fos.write(b, 0, len);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}