JAVA网络编程初步

客户端

import java.net.Socket;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
import java.net.UnknownHostException;

public class ClientText {

      public static final String ip="127.0.32.42";
      public static int port=7890;
      private Socket client_s;

      public ClientText(){
        try{
          client_s=new Socket(ip,port);
        }catch(UnknownHostException e){
          e.printStackTrace();
        }catch(IOException c){
          c.printStackTrace();
        }
      }

      public void setConnection() throws IOException{
        InputStream is;
        OutputStream os;
        try{
          is=client_s.getInputStream();
          os=client_s.getOutputStream();
          BufferedReader sys_br=new BufferedReader(new InputStreamReader(System.in));
          BufferedReader client_br=new BufferedReader(new InputStreamReader(is)); 
          PrintWriter client_pw=new PrintWriter(os,true);
           String warnStr=null;
          if((warnStr=client_br.readLine())!=null){
            System.out.println(warnStr);
          }
          while((warnStr=sys_br.readLine())!=null){
            client_pw.println(warnStr);
            break;
            }
        }catch(IOException e){
              e.printStackTrace();
        }
      }

    public static void main(String[] args) {
        // TODO 自动生成的方法存根
         ClientText myclient=new ClientText();
            try {
                myclient.setConnection();
            } catch (IOException e) {
                // TODO 自动生成的 catch 块
                e.printStackTrace();
            }
    }

}

服务端

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.io.BufferedReader;

public class ServerText{

  private ServerSocket ss; 
  public static final int port=7890;

  public ServerText(){
    try{
      ss=new ServerSocket(port);
    }catch(IOException e){
      e.printStackTrace();
    }
  }

  public void setConnection()throws IOException{
    Socket server_s;
    OutputStream os;
    InputStream is;
    try{
      //服务器程序陷入阻塞状态,随时接受客户端的链接
      server_s=ss.accept();
      //接收并连接之后
      os=server_s.getOutputStream();
      is=server_s.getInputStream();
      //给客户端发送数据
      PrintWriter server_pw=new PrintWriter(os,true);
      //接收客户端的数据
      BufferedReader server_br=new BufferedReader(new InputStreamReader(is));
      String msg=null;
      server_pw.println("连接成功,请输入一些数据传递到服务端");
      if((msg=server_br.readLine())!=null){
        System.out.println("客户端传递过来的数据为:"+msg);
        server_pw.println("数据传输成功");
      }else{
        server_pw.println("数据传输失败,出现错误");
      }
    }catch(IOException e){
      e.printStackTrace();
    } 
  }

  public static void main(String args[]) throws IOException{
         ServerText server_text=new ServerText();
         server_text.setConnection();

      }  
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值