实例简介:服务器(手机、pad等移动终端) / 客户端(嵌入式终端、为方便调试使用pc的ubuntu系统调试)
服务器:移动终端上选取颜色后,将其RGB值通过wifi方式传送到到嵌入式终端,使用TCP协议传输:
public Handler retSetViewHandler = new Handler() {
@SuppressWarnings("static-access")
@Override
public void handleMessage(Message msg) {
// TODO Auto-generated method stub
super.handleMessage(msg);
switch (which_picture) {
case 0:
RGB_Value[0] = (int) ((Color.red(color) * seekbarNum) / 100);
RGB_Value[1] = (int) ((Color.green(color) * seekbarNum) / 100);
RGB_Value[2] = (int) ((Color.blue(color) * seekbarNum) / 100);
break;
case 1:
RGB_Value[0] = (int) ((255 * seekbarNum) / 100);
RGB_Value[1] = (int) ((255 * seekbarNum) / 100);
RGB_Value[2] = (int) ((1 * seekbarNum) / 100);
break;
case 2:
RGB_Value[0] = (int) ((255 * seekbarNum) / 100);
RGB_Value[1] = (int) ((255 * seekbarNum) / 100);
RGB_Value[2] = (int) ((255 * seekbarNum) / 100);
break;
default:
break;
}
color_argb = (int) ((255 * seekbarNum) / 100);
Color backGroundColor = new Color();
img_currcolor.setBackgroundColor(backGroundColor.argb(color_argb,
RGB_Value[0], RGB_Value[1], RGB_Value[2]));
viewofr.setText(RGB_Value[0] + "");
viewofg.setText(RGB_Value[1] + "");
viewofb.setText(RGB_Value[2] + "");
RGBdata.RGBClass_Num1.colorValueofR_int = RGB_Value[0];
RGBdata.RGBClass_Num1.colorValueofG_int = RGB_Value[1];
RGBdata.RGBClass_Num1.colorValueofB_int = RGB_Value[2];
RGBdata.RGBClass_Num1.InitSetDataGroup();
try {
if (socket.isConnected()) {
if (!socket.isOutputShutdown()) {
out.println(RGB_Value[0]+" "+ RGB_Value[1]+" "+RGB_Value[2]);
}
}
} catch (Exception e) {
}
seekbarPencentShow.setText(seekbarNum + "%");
}
};
public Handler seekBarHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
}
};
ubuntu下客户端的c代码(测试用);
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<errno.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
int main(int argc, char** argv)
{
int listenfd, connfd;
struct sockaddr_in servaddr = {0};
char rbuf[128];
int n,i,red;
if( (listenfd = socket(AF_INET, SOCK_STREAM, 0)) == -1 )
{
perror("Create TCP Socket");
return -1;
}
servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
servaddr.sin_port = htons(2012);//端口2012
//unsigned int value=0x1;
// setsockopt(servaddr,SOL_SOCKET,SO_REUSEADDR,(void*)&value,sizeof(value));
if( bind(listenfd, (struct sockaddr*)&servaddr, sizeof(servaddr)) == -1)
{
perror("bind socket error:");
return -1;
}
if( listen(listenfd, 10) == -1)
{
perror("listen socket error:");
return -1;
}
printf("======waiting for client's request======\n");
while(1)
{
if( (connfd = accept(listenfd, (struct sockaddr*)NULL, NULL)) == -1)
{
perror("accept socket error:");
continue;
}
while(1)
{
n = recv(connfd, rbuf, sizeof(rbuf), 0);
//printf("n=%d\n", n);
if(n>0)
{
rbuf[n] = '\0';
printf("RED: %d \n", (rbuf[0]-0x30)*100+(rbuf[1]-0x30)*10+(rbuf[2]-0x30));//red的值(int类型)
printf("GREEN: %d \n ", (rbuf[4]-0x30)*100+(rbuf[5]-0x30)*10+(rbuf[6]-0x30));
printf("BLUE: %d \n", (rbuf[8]-0x30)*100+(rbuf[9]-0x30)*10+(rbuf[10]-0x30));
printf("recvmsg from client: %s ", rbuf);
}//RGB字符串
else if(n < 0)
{
perror("recv error:");
close(connfd);
break;
}
}
close(connfd);
}
close(listenfd);
return 0;
}