java网络socket编程

 

public class Client {

public static void main(String[] args) {

 

Socket socket;

try {

//这一句代表连接上了

Socket s_sid_socket = new Socket("192.168.106.107",5678);

 

 //结果处理 

OutputStream outputStream= s_sid_socket .getOutputStream();

BufferedWriter bufferWriter=new BufferedWriter(new OutputStreamWriter(outputStream));

bufferWriter.write("Client write");

bufferWriter.flush();

//必须关闭。不关闭服务器端会出现connect reset异常

s_sid_socket .close();

} catch (UnknownHostException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

 

}

}

 

 

 

public class Server {

public static void main(String[] args) {

try {

//这一句代表连接上了

Socket c_sid_socket = new ServerSocket(5678).accept();

 

 //结果处理

InputStream inputStream=c_sid_socket.getInputStream();

BufferedReader bufferedReader=new BufferedReader(new InputStreamReader(inputStream));

System.out.println(bufferedReader.readLine());

 

 

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

}

我的话费充值店-各种面额
电信100元仅售98.60 

联通100仅售99.00
移动100仅售99.30

你可能感兴趣的:(socket编程)