import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.nio.channels.Pipe;
/**
* Author:kson
* E-mail:19655910@qq.com
* Time:2017/08/11
* Description:服务端
*/
public class Server {
public static void main(String args[]){
try {
//创建ServerSocket对象,绑定端口号
ServerSocket serverSocket = new ServerSocket(8999);
while (true){
Socket socket = serverSocket.accept();//阻塞方法
new MyThread(socket).start();
}
} catch (IOException e) {
e.printStackTrace();
}finally {
}
}
static class MyThread extends Thread{
private Socket mSocket;
InputStream in = null;
BufferedReader bufferReader = null;
OutputStream out = null;
public MyThread(Socket socket){
this.mSocket = socket;
}
@Override
public void run() {
try {
in = mSocket.getInputStream();
System.out.println("连接成功,客户端的ip地址是:"+mSocket.getInetAddress());
StringBuffer sb = new StringBuffer();
bufferReader = new BufferedReader(new InputStreamReader(in));
String line = null;
while ((line=bufferReader.readLine())!=null){
sb.append(line);
}
System.out.println("欢迎客户端加入:"+sb.toString());
//得到流通道里的输出流,用于给服务端发送数据
String response = "我是服务端,你好"+sb.toString();
out = mSocket.getOutputStream();
out.write(response.getBytes(),0,response.getBytes().length);
out.flush();
mSocket.shutdownOutput();
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
out.close();
bufferReader.close();
in.close();
mSocket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
clent
package com.kson.socketdemo;
import Java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.NET.Socket;
/**
* Author:kson
* E-mail:19655910@qq.com
* Time:2017/08/11
* Description:客户端
*/
public class Client {
public static void main(String[] args) {
Socket socket = null;
InputStream in = null;
BufferedReader bufferReader = null;
OutputStream out = null;
try {
//建立客户端socket对象,绑定ip地址和端口号,和服务端建立唯一的通道
socket = new Socket("169.254.209.59", 8999);
//客户端传输的数据
String name = "kson";
//得到流通道里的输出流,用于给服务端发送数据
out = socket.getOutputStream();
out.write(name.getBytes(), 0, name.getBytes().length);
out.flush();
socket.shutdownOutput();//标示,代表我已经发送完毕了,服务端,你可以读取完整的数据了
//得到流通道里的输入流,用于接收服务器返回的数据,并逐行读取输入流数据,转换成字符串
in = socket.getInputStream();
StringBuffer sb = new StringBuffer();
bufferReader = new BufferedReader(new InputStreamReader(in));
String line = null;
while ((line = bufferReader.readLine()) != null) {
sb.append(line);
}
System.out.println("我是服务端,我已经收到你的信息:" + name);
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
bufferReader.close();
in.close();
out.close();
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
Main
package com.kson.socketdemo;import Android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.Socket;
public class MainActivity extends AppCompatActivity {
private EditText mNameEt;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
}
private void initView() {
mNameEt = (EditText) findViewById(R.id.et_name);
}
public void sendClick(View v){
final String name = mNameEt.getText().toString();
new Thread(new Runnable() {
@Override
public void run() {
sendMsg(name);
}
}).start();
}
private void sendMsg(String name) {
Socket socket = null;
InputStream in = null;
BufferedReader bufferReader = null;
OutputStream out = null;
try {
//建立客户端socket对象,绑定ip地址和端口号,和服务端建立唯一的通道
socket = new Socket("169.254.209.59", 8999);
//客户端传输的数据
// String name = "kson";
//得到流通道里的输出流,用于给服务端发送数据
out = socket.getOutputStream();
out.write(name.getBytes(), 0, name.getBytes().length);
out.flush();
socket.shutdownOutput();//标示,代表我已经发送完毕了,服务端,你可以读取完整的数据了
//得到流通道里的输入流,用于接收服务器返回的数据,并逐行读取输入流数据,转换成字符串
in = socket.getInputStream();
StringBuffer sb = new StringBuffer();
bufferReader = new BufferedReader(new InputStreamReader(in));
String line = null;
while ((line = bufferReader.readLine()) != null) {
sb.append(line);
}
System.out.println("我是服务端,我已经收到你的信息:" + name);
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
bufferReader.close();
in.close();
out.close();
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}