Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/298.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/216.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/api/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#和Android套接字图像发送_C#_Android_Image_Sockets - Fatal编程技术网

C#和Android套接字图像发送

C#和Android套接字图像发送,c#,android,image,sockets,C#,Android,Image,Sockets,我正在尝试为android做一个新的应用程序。我通过套接字将我的C#应用程序与Android应用程序连接起来,我可以轻松发送文本。然而,我不能做的是发送图片。客户端Android应用程序将发送文本,我的桌面应用程序将用位图图像进行应答。我尝试了Base64编码,但它对我不起作用。我卡住了 C#服务器响应代码; tobase64字符串函数 Android客户端 public void addListener(OnListener侦听器){ this.listener=listener; } 静态处

我正在尝试为android做一个新的应用程序。我通过套接字将我的C#应用程序与Android应用程序连接起来,我可以轻松发送文本。然而,我不能做的是发送图片。客户端Android应用程序将发送文本,我的桌面应用程序将用位图图像进行应答。我尝试了Base64编码,但它对我不起作用。我卡住了

C#服务器响应代码tobase64字符串函数 Android客户端
public void addListener(OnListener侦听器){
this.listener=listener;
}
静态处理程序=新处理程序(){
@凌驾
公共无效handleMessage(消息消息消息){
国际单项体育联合会(旗){
bmp=SixtyFourBaseToBitmap(msg.obj.toString());
如果(bmp!=null)
{
设置图像位图(bmp);
}
其他的
{
kq+=msg.obj.toString()+“\r\n”;
textResponse.setText(kq);
}
}
}
};
公共静态位图SixtyFourBaseToBitmap(字符串编码字符串)
{
尝试
{
字节[]decodedString=Base64.decode(codedString,Base64.DEFAULT);
位图decodedByte=BitmapFactory.decodeByteArray(decodedString,0,decodedString.length);
返回解码字节;
}
捕获(例外e)
{
返回null;
}
}
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
buttonConnect=(ImageButton)findViewById(R.id.connect_按钮);
设置按钮(ImageButton)findViewById(R.id.settings按钮);
textResponse=(TextView)findViewById(R.id.response);
imgView=(ImageView)findViewById(R.id.response\u img);
preferences=PreferenceManager.GetDefaultSharedReferences(getApplicationContext());
editor=preferences.edit();
ipbilgisi=preferences.getString(“ip”、“ip Girin”);
portBilgisi=preferences.getString(“port”、“port Girin”);
buttonConnect.setOnClickListener(新的OnClickListener(){
@凌驾
公共void onClick(视图v){
//TODO自动生成的方法存根
myClientTask=新的ClientTask(ipbilgisi、portBilgisi);
myClientTask.execute();
}
});
}
公共空间设置(视图五)
{
意图i=新意图(此,Settings.class);
星触觉(i);
完成();
}
公共类ClientTask扩展了AsyncTask实现
在线监听器{
字符串地址;
国际数据传输端口;
打印输出1;
ClientTask(字符串地址、字符串端口){
dstAddress=addr;
dstPort=Integer.parseInt(端口);
}
@凌驾
受保护的void onProgressUpdate(字符串…值){
//TODO自动生成的方法存根
super.onProgressUpdate(值);
}
@凌驾
受保护的字符串doInBackground(字符串…参数){
//TODO自动生成的方法存根
试一试{
套接字=新的套接字(dstAddress,dstPort);
out1=新的PrintWriter(socket.getOutputStream(),true);
//out1.打印(“你好服务器!”);
out1.flush();
BufferedReader in1=新的BufferedReader(新的InputStreamReader(
getInputStream());
做{
试一试{
如果(!in1.ready()){
如果(消息!=null){
MainActivity.handler.ActainMessage(0,0,-1,
服务器:“+消息).发送目标();
message=“”;
}
}
int num=in1.read();
message+=Character.toString((char)num);
}catch(异常类not){
}
}而(!message.equals(“再见”);
试一试{
发送信息(“再见”);
}捕获(例外e){
//TODO自动生成的捕捉块
e、 printStackTrace();
}最后{
socket.close();
}
}捕获(IOE异常){
e、 printStackTrace();
}
返回null;
}
@凌驾
受保护的void onPostExecute(字符串结果){
试一试{
if(socket.isClosed()){
flag=false;
}
}捕获(例外e){
Toast.makeText(getApplicationContext(),“BağlantıHatası!”,Toast.LENGTH_LONG.show();
}
super.onPostExecute(结果);
}
@凌驾
公共void侦听器(字符串文本){
//TODO自动生成的方法存根
发送消息(文本);
}
无效发送消息(字符串消息){
试一试{
输出1.打印(msg);
out1.flush();
如果(!msg.equals(“bye”))
MainActivity.handler.ActainMessage(0,0,-1,“Me:+msg)
.sendToTarget();
其他的
MainActivity.handler.ActainMessage(0,0,-1,
“Ngắt kết nối!”).sendToTarget();
}捕获(异常ioException){
ioException.printStackTrace();
}
}
}
公共无效发送(视图五){
addListener(myClientTask);
if(侦听器!=null)
listener.listener(((EditText)findViewById(R.id.editText1))
.getText().toString());
}
@凌驾
受保护的空onDestroy(){
//TODO自动生成的方法存根
试一试{
if(侦听器!=null)
倾听者。倾听者(“再见”);
socket.close();
}捕获(例外e){
//TODO:处理异常
}
super.ondestory();
}
@凌驾
受保护的void onStop(){
//TODO自动生成的方法存根
试一试{
if(侦听器!=null)
倾听者。倾听者(“再见”);
socket.close();
}捕获(例外e){
//TODO:处理异常
}
super.onStop();
}
公共void onClick(视图v){
意向意向=新意向(getApplicationContext(),
else if(text.ToLower() == "resim")
{
    response = "resim gönderiliyor...";
    byte[] text_ = Encoding.UTF8.GetBytes(response);
    socket.BeginSend(text_, 0, text_.Length, SocketFlags.None, new AsyncCallback(SendCallback), socket);
    Thread.Sleep(3000);

    byte[] buffer = ToBase64String(ScreenCaptured(), ImageFormat.Bmp);
    socket.BeginSend(buffer, 0, buffer.Length, SocketFlags.None, new AsyncCallback(SendCallback), socket);
}
public string ToBase64String(Bitmap bmp, ImageFormat imageFormat)
    {
        string base64String = string.Empty;


        MemoryStream memoryStream = new MemoryStream();
        bmp.Save(memoryStream, imageFormat);


        memoryStream.Position = 0;
        byte[] byteBuffer = memoryStream.ToArray();


        memoryStream.Close();


        base64String = Convert.ToBase64String(byteBuffer);
        byteBuffer = null;


        return base64String;
    }
public void addListener(OnListener listener) {
    this.listener = listener;
}

static Handler handler = new Handler() {
    @Override
    public void handleMessage(Message msg) {
        if (flag) {
            bmp = SixtyFourBaseToBitmap(msg.obj.toString());
            if(bmp != null)
            {
                imgView.setImageBitmap(bmp);
            }
            else
            {
                kq += msg.obj.toString() + "\r\n";
                textResponse.setText(kq);
            }
        }

    }
};
public static Bitmap SixtyFourBaseToBitmap(String codedString)
{
    try
    {
        byte[] decodedString = Base64.decode(codedString, Base64.DEFAULT);
        Bitmap decodedByte = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length);

        return decodedByte;
    }
    catch (Exception e)
    {
        return null;
    }
}
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    buttonConnect = (ImageButton) findViewById(R.id.connect_button);
    settings_button_ = (ImageButton) findViewById(R.id.settings_button);
    textResponse = (TextView) findViewById(R.id.response);
    imgView = (ImageView) findViewById(R.id.response_img);

    preferences= PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
    editor = preferences.edit();
    ipbilgisi=preferences.getString("ip", "IP Girin");
    portBilgisi= preferences.getString("port", "Port Girin");


    buttonConnect.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            myClientTask = new ClientTask(ipbilgisi, portBilgisi);
            myClientTask.execute();
        }
    });

}
public void goSettings(View v)
{
    Intent i = new Intent(this, Settings.class);
    startActivity(i);
    finish();
}
public class ClientTask extends AsyncTask<String, String, String> implements
        OnListener {

    String dstAddress;
    int dstPort;
    PrintWriter out1;


    ClientTask(String addr, String port) {
        dstAddress = addr;
        dstPort = Integer.parseInt(port);
    }

    @Override
    protected void onProgressUpdate(String... values) {
        // TODO Auto-generated method stub
        super.onProgressUpdate(values);

    }

    @Override
    protected String doInBackground(String... params) {
        // TODO Auto-generated method stub

        try {

            socket = new Socket(dstAddress, dstPort);
            out1 = new PrintWriter(socket.getOutputStream(), true);
            //out1.print("Hello server!");
            out1.flush();

            BufferedReader in1 = new BufferedReader(new InputStreamReader(
                    socket.getInputStream()));
            do {
                try {
                    if (!in1.ready()) {
                        if (message != null) {
                            MainActivity.handler.obtainMessage(0, 0, -1,
                                    "Server: " + message).sendToTarget();
                            message = "";
                        }
                    }
                    int num = in1.read();
                    message += Character.toString((char) num);
                } catch (Exception classNot) {
                }

            } while (!message.equals("bye"));

            try {
                sendMessage("bye");
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } finally {
                socket.close();
            }

        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
    @Override
    protected void onPostExecute(String result) {
        try {
            if (socket.isClosed()) {
                flag = false;
            }
        } catch (Exception e) {
            Toast.makeText(getApplicationContext(), "Bağlantı Hatası!", Toast.LENGTH_LONG).show();
        }

        super.onPostExecute(result);
    }

    @Override
    public void listener(String text) {
        // TODO Auto-generated method stub
        sendMessage(text);
    }

    void sendMessage(String msg) {
        try {
            out1.print(msg);
            out1.flush();
            if (!msg.equals("bye"))
                MainActivity.handler.obtainMessage(0, 0, -1, "Me: " + msg)
                        .sendToTarget();
            else
                MainActivity.handler.obtainMessage(0, 0, -1,
                        "Ngắt kết nối!").sendToTarget();
        } catch (Exception ioException) {
            ioException.printStackTrace();
        }
    }

}

public void send(View v) {
    addListener(myClientTask);
    if (listener != null)
        listener.listener(((EditText) findViewById(R.id.editText1))
                .getText().toString());
}

@Override
protected void onDestroy() {
    // TODO Auto-generated method stub
    try {
        if (listener != null)
            listener.listener("bye");
        socket.close();
    } catch (Exception e) {
        // TODO: handle exception
    }
    super.onDestroy();
}

@Override
protected void onStop() {
    // TODO Auto-generated method stub
    try {
        if (listener != null)
            listener.listener("bye");
        socket.close();
    } catch (Exception e) {
        // TODO: handle exception
    }
    super.onStop();
}

public void onClick(View v) {
    Intent intent = new Intent(getApplicationContext(), MainActivity.class);
    startActivity(intent);
    finish();
}