このコードは機能していません。ここで何が問題なのかわかりますか?開始されていますが、最初にスレッド化されたforループの前にプロセスが停止します。
Wi-Fiまたはワイヤレスデータ転送で使用するコードを作成しようとしています。ワイヤレスデータ通信でデータが失われるためです。
サーバーコード:
public class Server {
public static void main(String[] args) throws Exception{
final fbufr a1 = null,a2 = null,a3 = null,a4 = null,a5 = null,a6 = null,a7 = null,a8 = null;
final byte[] dd = new byte[1024];
ServerSocket servsock = new ServerSocket(5001);
int pos=0;
final File myFile = new File("Music.mp4");
final int packetsize=1024;
final double nosofpackets=Math.ceil(((int) myFile.length())/packetsize);
System.out.println(nosofpackets);
final Socket sock = servsock.accept();
System.out.println("server up!!");
final OutputStream os = sock.getOutputStream();
Thread t1 = new Thread(){
public void run(){
try{
System.out.println("thread1 up!!!");
int isbyte;
int checksum=0;
double i=0;
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(myFile));
System.out.println("near loop 1!!!");
for(i=0;i<nosofpackets+1;i=i+8)
{
a1.flag=0;
a2.flag=0;
a3.flag=0;
a4.flag=0;
a5.flag=0;
a6.flag=0;
a7.flag=0;
a8.flag=0;
isbyte = bis.read(a1.arr, 0,a1.arr.length);
System.out.println("Packet:"+(i+1)+"ready");
checksum += isbyte;
isbyte=bis.read(a2.arr, 0,a2.arr.length);
System.out.println("Packet:"+(i+2)+"ready");
checksum += isbyte;
isbyte=bis.read(a3.arr, 0,a3.arr.length);
System.out.println("Packet:"+(i+3)+"ready");
checksum += isbyte;
isbyte=bis.read(a4.arr, 0,a4.arr.length);
System.out.println("Packet:"+(i+4)+"ready");
checksum += isbyte;
isbyte=bis.read(a5.arr, 0,a5.arr.length);
System.out.println("Packet:"+(i+5)+"ready");
checksum += isbyte;
isbyte=bis.read(a6.arr, 0,a6.arr.length);
System.out.println("Packet:"+(i+6)+"ready");
checksum += isbyte;
isbyte=bis.read(a7.arr, 0,a7.arr.length);
System.out.println("Packet:"+(i+7)+"ready");
checksum += isbyte;
isbyte=bis.read(a8.arr, 0,a8.arr.length);
System.out.println("Packet:"+(i+8)+"ready");
checksum += isbyte;
for(int j=1 ;j<=8;j++){
send(j);
// Thread.sleep(500);
}
//Thread.sleep(1000);
checkdat(1);
//Thread.sleep(10000);
}
checkdat(1);
System.out.println(checksum+"data sent");
}catch (Exception e) {
// TODO: handle exception
}
}
private void checkdat(int j) throws Exception {
// TODO Auto-generated method stub
switch(j){
case 1 : if(a1.flag==0){ send(1); }
case 2 : if(a2.flag==0){ send(2); }
case 3 : if(a3.flag==0){ send(3); }
case 4 : if(a4.flag==0){ send(4); }
case 5 : if(a5.flag==0){ send(5); }
case 6 : if(a6.flag==0){ send(6); }
case 7 : if(a7.flag==0){ send(7); }
case 8 : if(a8.flag==0){ send(8); }
break;
default : break;
}
}
private void send(int j) throws Exception {
// TODO Auto-generated method stub
setbufr(j);
os.write(dd, 0, packetsize);
System.out.println("packet "+j+" recieved");
os.flush();
}
private void setbufr(int j) {
// TODO Auto-generated method stub
switch(j){
case 1 : System.arraycopy(a1.arr, 0, dd, 0, packetsize);
break;
case 2 : System.arraycopy(a2.arr, 0, dd, 0, packetsize);
break;
case 3 : System.arraycopy(a3.arr, 0, dd, 0, packetsize);
break;
case 4 : System.arraycopy(a4.arr, 0, dd, 0, packetsize);
break;
case 5 : System.arraycopy(a5.arr, 0, dd, 0, packetsize);
break;
case 6 : System.arraycopy(a6.arr, 0, dd, 0, packetsize);
break;
case 7 : System.arraycopy(a7.arr, 0, dd, 0, packetsize);
break;
case 8 : System.arraycopy(a8.arr, 0, dd, 0, packetsize);
break;
default : break;
}
}
};
Thread t2 = new Thread(){
public void run(){
try{
BufferedReader br = new BufferedReader(new InputStreamReader(sock.getInputStream()));
Integer da = Integer.parseInt(br.readLine());
donef(da);
}catch (Exception e) {
// TODO: handle exception
}
}
private void donef(Integer da) {
// TODO Auto-generated method stub
switch(da){
case 1 : a1.flag=1;
break;
case 2 : a2.flag=1;
break;
case 3 : a3.flag=1;
break;
case 4 : a4.flag=1;
break;
case 5 : a5.flag=1;
break;
case 6 : a6.flag=1;
break;
case 7 : a7.flag=1;
break;
case 8 : a8.flag=1;
break;
default : break;
}
}
};
t1.start();
t1.setPriority(10);
t2.start();
//sock.close();
}
}
クライアントコード:
public class client {
public static void main(String[] args) throws Exception{
final fbufr a1 = null,a2 = null,a3 = null,a4 = null,a5 = null,a6 = null,a7 = null,a8 = null;
final byte[] dd = new byte[1024];
int pos=0;
final int packetsize=1024;
Scanner s= new Scanner(System.in);
final double nosofpackets=s.nextDouble();
//final double nosofpackets=Math.ceil(((int) myFile.length())/packetsize);
System.out.println(nosofpackets);
final Socket sock = new Socket("127.0.0.1", 5001);
final OutputStream os = sock.getOutputStream();
final FileOutputStream fos = new FileOutputStream("Music.mp4");
final InputStream bis = sock.getInputStream();
//Timer tym1,tym2;
//Timer tym1= new Timer(){};
Thread t1 = new Thread(){
public void run(){
try{
int isbyte=0;
for(double i=0;i<nosofpackets+1;i++)
{
isbyte = bis.read(dd, 0,dd.length);if(isbyte==-1)System.out.println("data received");
setbufr((int)i%8);
}
}catch (Exception e) {
// TODO: handle exception
}
}
private void rece(int j) throws Exception {
// TODO Auto-generated method stub
setbufr(j);
bis.read(dd, 0, packetsize);
System.out.println("packet "+j+" recieved");
os.flush();
}
private void setbufr(int j) {
// TODO Auto-generated method stub
switch(j){
case 1 : System.arraycopy(dd, 0, a1.arr, 0, packetsize);a1.flag=1;
break;
case 2 : System.arraycopy(dd, 0, a2.arr, 0, packetsize);a2.flag=1;
break;
case 3 : System.arraycopy(dd, 0, a3.arr, 0, packetsize);a3.flag=1;
break;
case 4 : System.arraycopy(dd, 0, a4.arr, 0, packetsize);a4.flag=1;
break;
case 5 : System.arraycopy(dd, 0, a5.arr, 0, packetsize);a5.flag=1;
break;
case 6 : System.arraycopy(dd, 0, a6.arr, 0, packetsize);a6.flag=1;
break;
case 7 : System.arraycopy(dd, 0, a7.arr, 0, packetsize);a7.flag=1;
break;
case 8 : System.arraycopy(dd, 0, a8.arr, 0, packetsize);a8.flag=1;
break;
default : break;
}
}
};
Thread t2 = new Thread(){
public void run(){
try{
int x = 0;
x=sendack();
int n=sack(x);
if((x!=0)&&(n==1)){
PrintWriter pwr = new PrintWriter(sock.getOutputStream());
pwr.println( x);
pwr.flush();
}
}catch (Exception e) {
// TODO: handle exception
}
}
private int sack(int x) {
// TODO Auto-generated method stub
switch(x){
case 1:return a1.m;
case 2:return a2.m;
case 3:return a3.m;
case 4:return a4.m;
case 5:return a5.m;
case 6:return a6.m;
case 7:return a7.m;
case 8:return a8.m;
default: break;
}
return 0;
}
private int sendack() {
// TODO Auto-generated method stub
switch(1){
case 1: if(a1.flag==1 && a1.c==false){a1.c=true;a1.m++;return 1;}
case 2: if(a2.flag==1 && a2.c==false){a2.c=true;a2.m++;return 2;}
case 3: if(a3.flag==1 && a3.c==false){a3.c=true;a3.m++;return 3;}
case 4: if(a4.flag==1 && a4.c==false){a4.c=true;a4.m++;return 4;}
case 5: if(a5.flag==1 && a5.c==false){a5.c=true;a5.m++;return 5;}
case 6: if(a6.flag==1 && a6.c==false){a6.c=true;a6.m++;return 6;}
case 7: if(a7.flag==1 && a7.c==false){a7.c=true;a7.m++;return 7;}
case 8: if(a8.flag==1 && a8.c==false){a8.c=true;a8.m++;return 8;}
break;
default: return 0;
}
return 0;
}
};
t1.start();
t2.start();
//sock.close();
}
}
fbufr.java:
public class fbufr {
byte[] arr=new byte[1024];
int flag;
boolean c;
int m;
//Timer tym= new Timer();
public fbufr(){
flag=0;
c=false;
m=0;
}
}