关于socket

由于网络上的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();
}
}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值