I want to send a simple string to a server on my desktop PC. Here is what I have on my PC:
public static void main(String[] args) {
System.out.println("Server Started");
Server server = new Server();
server.start();
}
public void start(){
try {
ServerSocket SRVSOCK = new ServerSocket(333);
Socket SOCK = SRVSOCK.accept();
InputStreamReader ir = new InputStreamReader(SOCK.getInputStream());
BufferedReader bf = new BufferedReader(ir);
String MESSAGE = bf.readLine();
System.out.println(MESSAGE);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
For my android tablet I have this in the onCreate()
:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Thread tthread = new Thread(new Runnable(){
@Override
public void run() {
Connect();
}});
}
public void Connect(){
try {
Socket SOCK = new Socket("10.0.0.3", 333);
PrintWriter pw = new PrintWriter(SOCK.getOutputStream());
pw.println("FROM ANDROID!");
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
I have seen you can create a new thread which you need (otherwise the app UI freezes), but it still does not send the text to my server, I have added the incoming and outgoing port in my windows firewall, and even tried to turn off the firewall, but still no luck..
The android code is running on a real physical tablet (Nexus 7 2013) and not an emulator.
What is wrong here?
This whats in my log cat when the app is opened
03-24 13:43:59.695: I/ActivityManager(768): START u0 {act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10200000 cmp=com.hashimo.mcpeworldconverter/.MainActivity bnds=[200,1314] [400,1590] (has extras)} from uid 10022 on display 0
03-24 13:43:59.780: I/ActivityManager(768): Start proc com.hashimo.mcpeworldconverter for activity com.hashimo.mcpeworldconverter/.MainActivity: pid=6724 uid=10140 gids={50140, 9997, 1028, 1015, 3003} abi=armeabi-v7a
03-24 13:44:00.338: I/ActivityManager(768): Displayed com.hashimo.mcpeworldconverter/.MainActivity: +592ms
You are declaring a Thread but you forgot to .start() it. So your code is not executed.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.