當前位置:編程學習大全網 - 編程語言 - Java 網絡編程

Java 網絡編程

import java.io.*;

import java.net.*;

public class Sendserver

{

public static int port = 3333;

public static void main(String[] args) throws IOException

{

ServerSocket s = new ServerSocket(port);

Socket d= s.accept();

System.out.println("客戶端連接成功");

DataInputStream input = new DataInputStream(d.getInputStream());

int bufferSize = 8192;

byte[] buf = new byte[bufferSize];

DataOutputStream fileOut = new DataOutputStream(new BufferedOutputStream(

new BufferedOutputStream(new FileOutputStream(

"2.mp3"))));

while (true)

{

int read = 0;

if (input != null)

read = input.read(buf);

if (read == -1)

break;

byte[] temp=new byte[read];

for(int i=0;i<read;i++)

temp[i]=buf[i];

fileOut.write(temp);

}

fileOut.close();

System.out.println("傳送完畢");

}

}

import java.io.*;

import java.net.*;

public class SendClient

{

public static void main(String[] args) throws UnknownHostException, IOException

{

try{

DataInputStream iinput = new DataInputStream(new BufferedInputStream(

new FileInputStream("1.mp3")));

InetAddress addr = InetAddress.getByName("localhost");

Socket f= new Socket(addr,3333);

OutputStream output=f.getOutputStream();

int bufferSize = 8192;

byte[] buf = new byte[bufferSize];

while (true)

{

int read = 0;

if (iinput != null)

read = iinput.read(buf);

if (read == -1)

break;

byte[] temp=new byte[read];

for(int i=0;i<read;i++)

temp[i]=buf[i];

output.write(temp);

output.flush();

}

iinput.close();

output.close();

}

catch (IOException e)

{

e.printStackTrace();

}

}

}

改動相當大,妳自己看看吧。

  • 上一篇:線上編程課哪個機構最好
  • 下一篇:高位下拉能代替引體向上嗎?高位下拉和引體向上的區別
  • copyright 2024編程學習大全網