日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 运维知识 > Android >内容正文

Android

android tcp判断服务器是否断开,Android tcp客户端连接,然后从java服务器断开连接...

發(fā)布時(shí)間:2023/12/2 Android 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 android tcp判断服务器是否断开,Android tcp客户端连接,然后从java服务器断开连接... 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

我在我的電腦上執(zhí)行java服務(wù)器并讓我的android設(shè)備連接到它。我可以連接它,但一旦連接就會(huì)斷開(kāi)連接。我確信在android方面我需要做一些事情,但我很少迷失它。我有互聯(lián)網(wǎng)許可,所以很好。Android tcp客戶端連接,然后從java服務(wù)器斷開(kāi)連接

而且忽視了注釋代碼,我發(fā)送圖像從我的Android到PC。也忽略savebitmap并采取截圖我不認(rèn)為它影響連接,如果它沒(méi)有被調(diào)用。

的Java服務(wù)器:Java服務(wù)器

import java.io.BufferedReader;

import java.io.DataOutputStream;

import java.io.InputStream;

import java.io.InputStreamReader;

import java.net.Socket;

public class EchoThread extends Thread {

protected Socket socket;

public EchoThread(Socket clientSocket){

this.socket = clientSocket;

}

public void run(){

InputStream inp = null;

BufferedReader brinp = null;

DataOutputStream out = null;

try{

inp = socket.getInputStream();

brinp = new BufferedReader(new InputStreamReader(inp));

out = new DataOutputStream(socket.getOutputStream());

}catch(Exception e){

return;

}

String line;

while(true){

try{

line = brinp.readLine();

if ((line == null) || line.equalsIgnoreCase("QUIT")){

socket.close();

return;

}else{

out.writeBytes(line+ "\n\r");

out.flush();

}

}catch(Exception e){

e.printStackTrace();

return;

}

}

}

}

這里

import javax.imageio.ImageIO;

import javax.imageio.ImageReadParam;

import javax.imageio.ImageReader;

import javax.imageio.stream.ImageInputStream;

import javax.swing.*; //access JFrame

import javax.swing.text.html.HTMLDocument.Iterator;

import java.awt.BorderLayout;

import java.awt.Graphics;

import java.awt.Graphics2D;

import java.awt.Image;

import java.awt.image.BufferedImage;

import java.io.*;

import java.net.*;

public class tcpmain {

BufferedReader in;

static final int PORT = 9000;

public static void main(String[] args) {

// TODO Auto-generated method stub

//lines to make the gui frame

JFrame mainframe = new JFrame();

mainframe.setTitle("TCP Listener");

mainframe.setSize(600,800);

mainframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

mainframe.setVisible(true);

ServerSocket serverSocket = null;

Socket socket = null;

try{

serverSocket = new ServerSocket(PORT);

//Receive code motherfucker

/*int filesize=450660;

int bytesRead;

int current=0;

//receive file motherfucker

byte [] mybytearray = new byte [filesize];

InputStream is = srsvsock.getInputStream();

FileOutputStream fos = new FileOutputStream("C:\\Users\\Andy\\Desktop\\testimage.jpg");

BufferedOutputStream bos = new BufferedOutputStream(fos);

bytesRead = is.read(mybytearray,0,mybytearray.length);

current = bytesRead;*/

/*do{

bytesRead =

is.read(mybytearray, current, (mybytearray.length-current));

if(bytesRead >= 0) current += bytesRead;

} while(bytesRead > -1);

bos.write(mybytearray, 0, current);

bos.flush();*/

}

catch(Exception e){

e.printStackTrace();

}

while (true) {

try {

socket = serverSocket.accept();

JOptionPane.showMessageDialog(null, "Client connected", "ALERT", JOptionPane.INFORMATION_MESSAGE);

} catch (IOException e) {

System.out.println("I/O error: " + e);

}

// new threa for a client

new EchoThread(socket).start();

if (socket != null && !socket.isClosed()){

try {

socket.close();

JOptionPane.showMessageDialog(null, "Client Disconnected", "ALERT", JOptionPane.INFORMATION_MESSAGE);

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

}

}

}

客戶端處理程序是Android客戶端:

package com.example.tcpsocket;

import java.io.BufferedInputStream;

import java.io.ByteArrayOutputStream;

import java.io.File;

import java.io.FileInputStream;

import java.io.FileNotFoundException;

import java.io.FileOutputStream;

import java.io.IOException;

import java.io.InputStream;

import java.io.OutputStream;

import java.net.InetAddress;

import java.net.Socket;

import android.os.Bundle;

import android.app.Activity;

import android.graphics.Bitmap;

import android.graphics.Bitmap.CompressFormat;

import android.util.Log;

import android.view.Menu;

import android.view.View;

import android.view.View.OnClickListener;

import android.widget.Button;

public class MainActivity extends Activity {

private Socket socket;

private static final int SERVERPORT = 9000;

private static final String SERVER_IP = "192.168.1.113";

Bitmap bitmap;

File imagePath;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

Button btnconnect = (Button)findViewById(R.id.button1);

btnconnect.setOnClickListener(connect);

}

private OnClickListener connect = new OnClickListener(){

@Override

public void onClick(View arg0) {

// TODO Auto-generated method stub

//Bitmap bitmap = takeScreenshot();

// saveBitmap(bitmap);

new Thread(new clienthread()).start();

}

};

public Bitmap takeScreenshot() {

View rootView = findViewById(android.R.id.content).getRootView();

rootView.setDrawingCacheEnabled(true);

return rootView.getDrawingCache();

}

public void saveBitmap(Bitmap bitmap){

File imagePath = new File("sdcard/screenshot.jpg");

FileOutputStream fos;

try {

fos = new FileOutputStream(imagePath);

bitmap.compress(CompressFormat.JPEG, 100, fos);

fos.flush();

fos.close();

new Thread(new clienthread()).start();

} catch (FileNotFoundException e) {

Log.e("GREC", e.getMessage(), e);

} catch (IOException e) {

Log.e("GREC", e.getMessage(), e);

}

}

@Override

public boolean onCreateOptionsMenu(Menu menu) {

// Inflate the menu; this adds items to the action bar if it is present.

getMenuInflater().inflate(R.menu.main, menu);

return true;

}

class clienthread implements Runnable {

@Override

public void run() {

// TODO Auto-generated method stub

try{

InetAddress serverAddr = InetAddress.getByName(SERVER_IP);

socket = new Socket(serverAddr, SERVERPORT);

/*File imagePath = new File("sdcard/screenshot.jpg");

byte[] mybytearray = new byte[450560];

FileInputStream fis = new FileInputStream(imagePath);

BufferedInputStream bis = new BufferedInputStream(fis);

bis.read(mybytearray,0,mybytearray.length);

OutputStream os = socket.getOutputStream();

os.write(mybytearray,0,mybytearray.length);

os.close();*/

}

catch(Exception e){

}

}

}

}

2013-11-26

andyADD

總結(jié)

以上是生活随笔為你收集整理的android tcp判断服务器是否断开,Android tcp客户端连接,然后从java服务器断开连接...的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。