public
void
send_toServer(SendObject so,Context context) {
try
{
oos.writeObject(so);
oos.flush();
oos.reset();
wait_Task task =
new
wait_Task(context);
task.execute();
}
catch
(NullPointerException e) {
Toast.makeText(
this
,
"서버가 열려있지 않습니다"
, Toast.LENGTH_LONG).show();
}
catch
(IOException e1) {
System.out.println(
"Send_toServer Object영역"
);
e1.printStackTrace();
}
}
private
class
wait_Task
extends
AsyncTask<Void,Void,Void >{
ProgressDialog dialog;
wait_Task(Context context){
dialog =
new
ProgressDialog(context) ;
}
@Override
protected
void
onPreExecute() {
dialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
dialog.setTitle(
"Wait Loading..."
);
dialog.setMessage(
"조금만 기다려주세요 ㅠ.ㅠ"
);
dialog.show();
super
.onPreExecute();
}
@Override
protected
Void doInBackground(Void... params) {
while
(!isreceived){
}
return
null
;
}
@Override
protected
void
onPostExecute(Void result) {
dialog.dismiss();
super
.onPostExecute(result);
}
}
class
ConnectTread
extends
Thread {
public
void
run() {
Myclient client =
new
Myclient();
client.connection();
}
}
public
void
println(
final
String msg){
handler.post(
new
Runnable() {
@Override
public
void
run() {
try
{
Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_LONG).show();
}
catch
(NullPointerException e){
System.out.println(
"서버 닫혀있음"
);
}
}
});
}
private
void
servercheck(){
handler.post(
new
Runnable() {
@Override
public
void
run() {
textview_serverstate.setText(
"made by 혜윰 , Server Open"
);
}
});
}
private
class
Myclient {
String ip =
"218.151.105.235"
;
int
port =
5001
;
private
void
connection() {
try
{
socket =
new
Socket(ip, port);
if
(socket !=
null
){
servercheck();
}
ois =
new
ObjectInputStream(socket.getInputStream());
oos =
new
ObjectOutputStream(socket.getOutputStream());
Thread th =
new
Thread(
new
Runnable() {
@Override
public
void
run() {
try
{
while
(
true
) {
so = (SendObject)ois.readObject();
isreceived =
true
;
System.out.println(
"서버로 부터 받은 메시지 : "
+so.msg);
new
Client_Protocol(so.msg);
}
}
catch
(ClassCastException e){
e.printStackTrace();
}
catch
(ClassNotFoundException e) {
e.printStackTrace();
}
catch
(EOFException e) {
e.printStackTrace();
}
catch
(SocketException e){
println(
"서버가 열려있지 않습니다."
);
try
{
oos.close();
ois.close();
socket.close();
}
catch
(IOException e1) {
e1.printStackTrace();
}
}
catch
(IOException e) {
try
{
oos.close();
ois.close();
socket.close();
e.printStackTrace();
}
catch
(IOException e1) {
e1.printStackTrace();
}
}
}
});
th.start();
}
catch
(UnknownHostException e) {
e.printStackTrace();
}
catch
(IOException e) {
e.printStackTrace();
}
}
}