chat客户端
程序员文章站
2022-03-15 21:57:48
...
import java.io.*;
import java.net.Socket;
import java.util.Scanner;
public class newclient {
private Socket socket;
public newclient() {
try {
socket=new Socket("localhost", 1234);
System.out.println("successful");
} catch (Exception e) {
e.printStackTrace();
}
}
public void start() {
Scanner in=new Scanner(System.in);
try {
OutputStream out=socket.getOutputStream();
PrintWriter pw=new PrintWriter(
new OutputStreamWriter(out,"UTF-8"),true);
pw.println(in.nextLine());//nickname
new Thread(new serverhandler()).start();
while (true) {
pw.println(in.nextLine());
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
new newclient().start();
}
class serverhandler implements Runnable{
@Override
public void run() {
// TODO Auto-generated method stub
try {
InputStream in=socket.getInputStream();
InputStreamReader isr=new InputStreamReader(in,"UTF-8");
BufferedReader bfr=new BufferedReader(isr);
while (true) {
System.out.println(bfr.readLine());
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
上一篇: Yii2查询where条件拼装详解
下一篇: 二分查找某个元素的下标——Pyton实现
推荐阅读
-
Java根据Request获取客户端IP
-
mysql客户端命令行的使用_MySQL
-
PHP5.3连接Oracle客户端及PDO_OCI模块的安装方法
-
asp.net中“从客户端中检测到有潜在危险的Request.Form值”错误的解决办法
-
利用Python中SocketServer 实现客户端与服务器间非阻塞通信
-
这样才能正确解锁MaxCompute客户端 windowsaccess阿里巴巴jdk
-
这样才能正确解锁MaxCompute客户端 windowsaccess阿里巴巴jdk
-
这样才能正确解锁MaxCompute客户端 windowsaccess阿里巴巴jdk
-
SpringCloud客户端的负载均衡Ribbon的实现
-
Java Socket编程服务器响应客户端实例代码