TCP-IP Connection Android App to C# Server -
so im trying comunicate dmx-interface pluged pc/host via xamarin android app. allready got server running, works console client wrote first. wrote app,using same way before, tcpclient , writing data via stream. because i'm running app in emulator use ip 10.0.2.2 connect, cannot establish connection. app crashes or timeout exeption. been trying , researching 2 days , i'm desperate, attach code here, hope can help
thanks, johannes
app:
public class mainactivity : activity { public stream stream; tcpclient client = null; protected override void oncreate(bundle bundle) { base.oncreate(bundle); setcontentview(resource.layout.main); //adding control elements resource.layout.main button @ = findviewbyid<button>(resource.id.at); button enter = findviewbyid<button>(resource.id.enter); button thru = findviewbyid<button>(resource.id.thru); button = findviewbyid<button>(resource.id.all); button 1 = findviewbyid<button>(resource.id.one); button 2 = findviewbyid<button>(resource.id.two); button 3 = findviewbyid<button>(resource.id.three); button 4 = findviewbyid<button>(resource.id.four); button 5 = findviewbyid<button>(resource.id.five); button 6 = findviewbyid<button>(resource.id.six); button 7 = findviewbyid<button>(resource.id.seven); button 8 = findviewbyid<button>(resource.id.eight); button 9 = findviewbyid<button>(resource.id.nine); button 0 = findviewbyid<button>(resource.id.zero); button comma = findviewbyid<button>(resource.id.comma); button connect = findviewbyid<button>(resource.id.connect); button clear = findviewbyid<button>(resource.id.clear); at.click += (o, e) => { updatecmdline("@"); }; clear.click += (o, e) => { flushcmdline(); }; enter.click += (o, e) => { textview cmdline = findviewbyid<textview>(resource.id.cmdline); processdata(cmdline.text); flushcmdline(); }; thru.click += (o, e) => { updatecmdline("/"); }; all.click += (o, e) => { updatecmdline("#"); }; one.click += (o, e) => { updatecmdline("1"); }; two.click += (o, e) => { updatecmdline("2"); }; three.click += (o, e) => { updatecmdline("3"); }; four.click += (o, e) => { updatecmdline("4"); }; five.click += (o, e) => { updatecmdline("5"); }; six.click += (o, e) => { updatecmdline("6"); }; seven.click += (o, e) => { updatecmdline("7"); }; eight.click += (o, e) => { updatecmdline("8"); }; nine.click += (o, e) => { updatecmdline("9"); }; zero.click += (o, e) => { updatecmdline("0"); }; comma.click += (o, e) => { updatecmdline(","); }; connect.click += (o, e) => { try { textview ip = findviewbyid<textview>(resource.id.ipfield); textview port = findviewbyid<textview>(resource.id.portfield); textview connectionstatus = findviewbyid<textview>(resource.id.connectedstatus); client = new tcpclient("10.0.2.2", 4711); connectionstatus.text = "connected"; } catch (system.exception) { } }; //implementing click functions } public void updatecmdline(string update) { textview cmdline = findviewbyid<textview>(resource.id.cmdline); cmdline.text += update; } public void flushcmdline() { textview cmdline = findviewbyid<textview>(resource.id.cmdline); cmdline.text = ""; } public int[] getdata(string channel, string value) { int[] data = new int[] {int32.parse(channel), int32.parse(value)}; return data; } public void senddata(int channel, int data) { if (client.connected) { stream = client.getstream(); byte[] outgoingbytes = new byte[] {convert.tobyte(channel),convert.tobyte(data)}; stream.write(outgoingbytes,0,outgoingbytes.length); } } public void processdata(string cmdline) { string[] dividestackedops = cmdline.split(','); foreach (string s in dividestackedops) { if (s.contains("/")) { string[] dividedthru = s.split('/'); int channel1 = int32.parse(dividedthru[0]); string[] dividedat = dividedthru[1].split('@'); int channel2 = int32.parse(dividedat[0]); int value = int32.parse(dividedat[1]); (int e = channel2; e >= channel1; e--) { senddata(e, value); } } else if (s.contains("#")) { string[]dividedall = s.split('#'); int value = int32.parse(dividedall[1]); (int e = 100; e > 0; e--) { senddata(e, value); } } } } }
}
server
public class dmxinterface { [dllimport("k8062d.dll")] public static extern void startdevice(); [dllimport("k8062d.dll")] public static extern void setdata(int channel, int data); [dllimport("k8062d.dll")] public static extern void stopdevice(); [dllimport("k8062d.dll")] public static extern void setchannelcount(int count); } class program { private static tcplistener listener; private static arraylist threads = new arraylist(); public static boolean serverstopped; public static void main() { dmxinterface.startdevice(); dmxinterface.setchannelcount(10); //initialize listener, start listener int port = 4711; ipaddress ip = ipaddress.parse("127.0.0.1"); listener = new tcplistener(ip, port); listener.start(); console.writeline("creating new listener on: "+ip+":"+port); //initialize mainserverthread, start mainserverthread console.writeline("starting mainthread"); thread mainthread = new thread(run); mainthread.start(); console.writeline("done"); while (!serverstopped) { console.writeline("write 'stop' stop server..."); string cmd = ""; cmd = console.readline(); if (cmd.tolower().equals("stop")) { console.writeline("stopping server"); serverstopped = true; } else { console.writeline("unknow command: " + cmd); } } endthreads(mainthread); } public static void endthreads(thread mainthread) { //stopping mainthread mainthread.abort(); console.writeline("mainthread stopped"); //stopping threads (ienumerator e = threads.getenumerator(); e.movenext();) { //getting next serverthread console.writeline("stopping serverthreads"); serverthread serverthread = (serverthread)e.current; //stop serverthread.stop = true; while (serverthread.running) { thread.sleep(1000); } } //stopping listener listener.stop(); console.writeline("listener stopped"); thread.sleep(5000); console.clear(); } //opening new serverthread public static void run() { while (true) { console.writeline("listener waiting connection wish"); //waiting incoming connection wish tcpclient client = listener.accepttcpclient(); threads.add(new serverthread(client)); } } } class serverthread { public bool stop; public bool running; private tcpclient client; public serverthread(tcpclient client) { this.client = client; //starting new thread run() function console.writeline("starting new serverthread"); new thread(new threadstart(run)).start(); } //threaded function public void run() { stream stream = null; boolean gotstream = false; console.writeline("reading incoming data stream"); running = true; while (!gotstream) { //making sure stream not null avoid exeption if (client.getstream() != null) { //getting stream, setting flag true stream = client.getstream(); gotstream = true; } } boolean loop = true; byte[] incomingvalues = new byte[] {255, 255}; boolean gotdata = false; while (loop) { //checking if remote host still connected if (client.connected) { try { //reading byte array stream.read(incomingvalues, 0, incomingvalues.length); gotdata = true; } catch (exception e) { //catching exeptions console.writeline("ooops, wrent wrogn \n" + e); loop = false; } if (gotdata) { //converting bytes int, sending dmx values int channel = convert.toint32(incomingvalues[0]); int value = convert.toint32(incomingvalues[1]); dmxinterface.setdata(channel, value); console.writeline("received data... \n channel: " + channel + " value: " + value); } } else { //exiting loop if connection lost console.writeline("connection lost"); program.serverstopped = true; loop = false; } } } }
}
Comments
Post a Comment