Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7569965
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T15:17:56+00:00 2026-05-30T15:17:56+00:00

i have written simple client server socket programming code as follow MyServer.java import java.io.DataInputStream;

  • 0

i have written simple client server socket programming code as follow

MyServer.java

 import java.io.DataInputStream;
 import java.io.DataOutputStream;
 import java.io.IOException;
 import java.net.ServerSocket;
 import java.net.Socket;


 public class MyServer {

   public static void main(String[] args){
   ServerSocket serverSocket = null;
   Socket socket = null;
   DataInputStream dataInputStream = null;
   DataOutputStream dataOutputStream = null;

    try {
         serverSocket = new ServerSocket(8888);
         System.out.println("Listening :8888");
        } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   }

   while(true){
    try {
    socket = serverSocket.accept();
    dataInputStream = new DataInputStream(socket.getInputStream());
    dataOutputStream = new DataOutputStream(socket.getOutputStream());
    System.out.println("ip: " + socket.getInetAddress());
    System.out.println("message: " + dataInputStream.readUTF());
    dataOutputStream.writeUTF("Hello!");
    } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   }
   finally{
   if( socket!= null){
 try {
  socket.close();
 } catch (IOException e) {
  // TODO Auto-generated catch block
  e.printStackTrace();
 }
}

if( dataInputStream!= null){
 try {
  dataInputStream.close();
 } catch (IOException e) {
  // TODO Auto-generated catch block
  e.printStackTrace();
 }
}

if( dataOutputStream!= null){
 try {
  dataOutputStream.close();
 } catch (IOException e) {
  // TODO Auto-generated catch block
  e.printStackTrace();
    }
   }
  }
 }
 }}

android client is as follows

MyClientActivity.java

package com.exercise.AndroidClient;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
import java.net.UnknownHostException;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.os.Bundle;

public class MyClientActivity extends Activity {
EditText textOut;
TextView textIn;

 /** Called when the activity is first created. */
 @Override
 public void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);
     setContentView(R.layout.main);

     textOut = (EditText)findViewById(R.id.textout);
     Button buttonSend = (Button)findViewById(R.id.send);
     textIn = (TextView)findViewById(R.id.textin);
     buttonSend.setOnClickListener(buttonSendOnClickListener);
 }

 Button.OnClickListener buttonSendOnClickListener
 = new Button.OnClickListener(){

@Override
public void onClick(View arg0) {
 // TODO Auto-generated method stub
 Socket socket = null;
 DataOutputStream dataOutputStream = null;
 DataInputStream dataInputStream = null;

 try {
  socket = new Socket("113.193.42.220", 8888);
  dataOutputStream = new DataOutputStream(socket.getOutputStream());
  dataInputStream = new DataInputStream(socket.getInputStream());
  dataOutputStream.writeUTF(textOut.getText().toString());
  textIn.setText(dataInputStream.readUTF());
 } catch (UnknownHostException e) {
  // TODO Auto-generated catch block
  e.printStackTrace();
 } catch (IOException e) {
  // TODO Auto-generated catch block
  e.printStackTrace();
 }
 finally{
  if (socket != null){
   try {
    socket.close();
   } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   }
  }

  if (dataOutputStream != null){
   try {
    dataOutputStream.close();
   } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   }
  }

  if (dataInputStream != null){
   try {
    dataInputStream.close();
   } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   }
  }
 }
}};
 }

Main.xml

 <?xml version="1.0" encoding="utf-8"?>
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:orientation="vertical"
 android:layout_width="fill_parent"
 android:layout_height="fill_parent"
 >
 <TextView
 android:layout_width="fill_parent"
 android:layout_height="wrap_content"
 android:text="@string/hello"
 />
 <EditText
 android:id="@+id/textout"
 android:layout_width="fill_parent"
 android:layout_height="wrap_content"
 />
 <Button
 android:id="@+id/send"
 android:layout_width="fill_parent"
 android:layout_height="wrap_content"
 android:text="Send"
 />
 <TextView
 android:id="@+id/textin"
 android:layout_width="fill_parent"
 android:layout_height="wrap_content"
 />
 </LinearLayout>

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.exercise.AndroidClient"
android:versionCode="1"
android:versionName="1.0" >
<uses-permission android:name="android.permission.INTERNET"/>

<uses-sdk android:minSdkVersion="15" />

<application
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name" >
    <activity
        android:name=".MyClientActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

</manifest>

I am getting FatalException on execution of client.pls suggest me how to handle this.
i have tried my pc’s ip address and 127.0.0.1 also so do i have any problem with ip address or anything else is causing FatalException pls help me out.

logcat is as follow

02-21 21:05:48.793: D/gralloc_goldfish(553): Emulator without GPU emulation detected.
02-21 21:08:39.794: D/AndroidRuntime(553): Shutting down VM
02-21 21:08:39.794: W/dalvikvm(553): threadid=1: thread exiting with uncaught exception (group=0x409c01f8)
02-21 21:08:39.824: E/AndroidRuntime(553): FATAL EXCEPTION: main
02-21 21:08:39.824: E/AndroidRuntime(553): android.os.NetworkOnMainThreadException
02-21 21:08:39.824: E/AndroidRuntime(553):  at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1099)
02-21 21:08:39.824: E/AndroidRuntime(553):  at libcore.io.BlockGuardOs.connect(BlockGuardOs.java:84)
02-21 21:08:39.824: E/AndroidRuntime(553):  at libcore.io.IoBridge.connectErrno(IoBridge.java:127)
02-21 21:08:39.824: E/AndroidRuntime(553):  at libcore.io.IoBridge.connect(IoBridge.java:112)
02-21 21:08:39.824: E/AndroidRuntime(553):  at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:192)
02-21 21:08:39.824: E/AndroidRuntime(553):  at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172)
02-21 21:08:39.824: E/AndroidRuntime(553):  at java.net.Socket.startupSocket(Socket.java:566)
02-21 21:08:39.824: E/AndroidRuntime(553):  at java.net.Socket.tryAllAddresses(Socket.java:127)
02-21 21:08:39.824: E/AndroidRuntime(553):  at java.net.Socket.<init>(Socket.java:177)
02-21 21:08:39.824: E/AndroidRuntime(553):  at java.net.Socket.<init>(Socket.java:149)
02-21 21:08:39.824: E/AndroidRuntime(553):  at com.exercise.AndroidClient.MyClientActivity$1.onClick(MyClientActivity.java:44)
02-21 21:08:39.824: E/AndroidRuntime(553):  at android.view.View.performClick(View.java:3511)
02-21 21:08:39.824: E/AndroidRuntime(553):  at android.view.View$PerformClick.run(View.java:14105)
02-21 21:08:39.824: E/AndroidRuntime(553):  at android.os.Handler.handleCallback(Handler.java:605)
02-21 21:08:39.824: E/AndroidRuntime(553):  at android.os.Handler.dispatchMessage(Handler.java:92)
02-21 21:08:39.824: E/AndroidRuntime(553):  at android.os.Looper.loop(Looper.java:137)
02-21 21:08:39.824: E/AndroidRuntime(553):  at android.app.ActivityThread.main(ActivityThread.java:4424)
02-21 21:08:39.824: E/AndroidRuntime(553):  at java.lang.reflect.Method.invokeNative(Native Method)
02-21 21:08:39.824: E/AndroidRuntime(553):  at java.lang.reflect.Method.invoke(Method.java:511)
02-21 21:08:39.824: E/AndroidRuntime(553):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
02-21 21:08:39.824: E/AndroidRuntime(553):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
02-21 21:08:39.824: E/AndroidRuntime(553):  at dalvik.system.NativeStart.main(Native Method)
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T15:17:57+00:00Added an answer on May 30, 2026 at 3:17 pm

    You are acessing network on main application thread. This is not allowed in Android 3+. You need to start separate thread do access network.

    You can use AsyncTask to separate your code, which is acessing network, to another Thread.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a very simple client server code written java(server listens on some port
I have a simple UDP client server written in C++ on Ubuntu 9.10 where
I have written a simple server - client program with Swing interface using singleton
I have a simple programs for socket client and server its not working over
I have written a simple TCP client and server. The problem lies with the
I am working on a simple client-server project. Client is written in Java, it
I have a very simple server written in C and an equally simple client
I have written a simple client server program in C under linux. I have
I have a written a simple Client-Server pair, sending an Object to the server.
I'm writing networking programming using socket.h to my studies. I have written server and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.