표준 자바 코드
package org.androidtown.socket;
import java.io.ObjectInputStream;
import java.io.ObjectOutput;
import java.io.ObjectOutputStream;
import java.net.ServerSocket;
import java.net.Socket;
//표준 자바에서 소켓 서버
// 소켓 서버 만들기
public class JavaSocketServer {
public static void main(String[] args) {
int port =5001;
try{
ServerSocket server=new ServerSocket(port);
System.out.println("서버시작됨 :" + port);
while(true){
Socket socket =server.accept();
//클라이언트 -> 서버 로 받은 데이터
ObjectInputStream instream =new ObjectInputStream(socket.getInputStream());
Object input=instream.readObject();
System.out.println("클라이언트로부턴 받은 데이터 : " + input);
// 서버 -> 클라이언트로 보내기
String output =input + " from Server." ;
ObjectOutputStream outputStream=new ObjectOutputStream(socket.getOutputStream());
outputStream.writeObject(output);
outputStream.flush();
System.out.println("클라이언트로 보낸 데이터 : " + output);
instream.close();
outputStream.close();
socket.close();
}
}catch (Exception e) {
e.printStackTrace();
}
}
}
class MainActivity
package kr.co.braverokmc.mysocketclient;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
public class MainActivity extends AppCompatActivity {
TextView textView;
EditText editText;
Handler handler=new Handler(){
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView=(TextView)findViewById(R.id.textView2);
editText=(EditText)findViewById(R.id.edit1);
}
public void onClick(View v){
if(editText.getText().toString().trim().length() < 1){
Toast.makeText(this, "전송할 문자를 입력하세요.", Toast.LENGTH_SHORT).show();
}else {
ConnectThread connectThread =new ConnectThread();
connectThread.setDaemon(true);
connectThread.start();
}
}
class ConnectThread extends Thread{
@Override
public void run() {
String host = "192.168.0.2";
int port = 5001;
try {
Socket socket = new Socket(host, port);
println("서버로 연결되었습니다. " + host + ", " + port);
String output =editText.getText().toString();
ObjectOutputStream outStream = new ObjectOutputStream(socket.getOutputStream());
outStream.writeObject(output);
outStream.flush();
println("서버로 보낸 데이터 : " + output);
ObjectInputStream instream = new ObjectInputStream(socket.getInputStream());
Object input = instream.readObject();
println("서버로 부터 받은 데이터 " + input);
instream.close();
outStream.close();
socket.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
private void println(final String data){
handler.post(new Runnable() {
@Override
public void run() {
textView.append(data +"\n");
}
});
}
}
R.layout.activity_main
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<Button
android:text="연결하기"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/button"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:onClick="onClick"
android:background="@android:color/holo_green_light"
android:textColor="@android:color/background_light" />
<EditText
android:hint="전송할 문자"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/edit1" />
<TextView
android:layout_marginTop="10dp"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/textView2"
android:textSize="24sp"
android:textColor="@android:color/holo_blue_dark" />
</LinearLayout>
AndroidMainfest.xml
<uses-permission android:name="android.permission.INTERNET" />
94강
95강
96강
97강
댓글 ( 4)
댓글 남기기