/*这是一个客户端程序*/
package com.client;
import java.io.*;
import java.net.*;
public class client {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
client t=new client();
}
public client()
{
try {
Socket s=new Socket("127.0.0.1",9991);
//如果s连接成功,就可以发送数据给服务器
//我们通过pw,向s发送数据,true表示及时刷新
PrintWriter pw=new PrintWriter(s.getOutputStream(),true);
pw.println("你好吗,我是客户端");
InputStreamReader isr=new InputStreamReader(s.getInputStream());
BufferedReader br=new BufferedReader(isr);
String info=br.readLine();
System.out.println(info);
}catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
}
/*
* 这是我的第一个服务器端程序,让他在9999端口监听
*/
package com.sever;
import java.io.*;
import java.net.*;
import javax.print.attribute.standard.Severity;
public class test1 {
public static void main(String[]args)
{
test1 x=new test1();
}
public test1()
{
ServerSocket ss;
try {
//在9999号端口监听
ss = new ServerSocket(9991);
//等待某个客户端来连接,该函数会返回一一个socket连接
Socket s=ss.accept();
InputStreamReader isr=new InputStreamReader(s.getInputStream());
BufferedReader br=new BufferedReader(isr);
String info=br.readLine();
System.out.println(info);
PrintWriter pw=new PrintWriter(s.getOutputStream(),true);
pw.println("我是服务器,你也好");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}