import java.io.*; import java.net.Socket; import java.util.Scanner; public class ChatClient { public static void main(String[] args) { try { Socket socket = new Socket("127.0.0.1",9090); new Thread(new Runnable() { @Override public void run() { InputStream inputStream = null; while(true) { try { inputStream = socket.getInputStream(); } catch (IOException e) { throw new RuntimeException(e); } Scanner out = new Scanner(inputStream); while(out.hasNextLine()) { System.out.println(out.nextLine()); } } } }).start(); new Thread(new Runnable() { @Override public void run() { while(true) { Scanner in = new Scanner(System.in); String string = in.nextLine(); try { OutputStream outputStream = socket.getOutputStream(); outputStream.write(string.getBytes(),0,string.length()); } catch (IOException e) { throw new RuntimeException(e); } if("bye".equals(string)) { break; } } } }).start(); } catch (IOException e) { throw new RuntimeException(e); } } }
import java.io.*;
import java.net.Socket;
import java.util.Scanner;
public class ChatClient
{
public static void main(String[] args)
{
try
{
Socket socket = new Socket("127.0.0.1",9090);
new Thread(new Runnable()
{
@Override
public void run()
{
InputStream inputStream = null;
while(true)
{
try
{
inputStream = socket.getInputStream();
}
catch (IOException e)
{
throw new RuntimeException(e);
}
Scanner out = new Scanner(inputStream);
while(out.hasNextLine())
{
System.out.println(out.nextLine());
}
}
}
}).start();
new Thread(new Runnable()
{
@Override
public void run()
{
while(true)
{
Scanner in = new Scanner(System.in);
String string = in.nextLine();
try
{
OutputStream outputStream = socket.getOutputStream();
outputStream.write(string.getBytes(),0,string.length());
}
catch (IOException e)
{
throw new RuntimeException(e);
}
if("bye".equals(string))
{
break;
}
}
}
}).start();
}
catch (IOException e)
{
throw new RuntimeException(e);
}
}
}文章来源:https://www.toymoban.com/news/detail-606325.html
import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.io.PrintStream; import java.net.InetAddress; import java.net.ServerSocket; import java.net.Socket; import java.util.ArrayList; public class ChatServer { public static void main(String[] args) { ArrayList<Socket> arrayList = new ArrayList<Socket>(); new Thread(new Runnable() { @Override public void run() { while(true) { try { arrayList.add(new ServerSocket(9090).accept()); } catch (IOException e) { throw new RuntimeException(e); } } } }).start(); new Thread(new Runnable() { @Override public void run() { while(true) { if(arrayList.size() != 0) { for(int i = 0; i < arrayList.size(); i++) { String ip = arrayList.get(i).getInetAddress().getHostAddress(); try { int len; byte[] bytes = new byte[10240]; while((len = arrayList.get(i).getInputStream().read()) != -1) { arrayList.get(i).getOutputStream().write(bytes,0,len); } } catch (IOException e) { throw new RuntimeException(e); } System.out.println(ip); } } if(arrayList.size() == 0) { System.out.println("No User"); } } } }).start(); } }
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintStream;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
public class ChatServer
{
public static void main(String[] args)
{
ArrayList<Socket> arrayList = new ArrayList<Socket>();
new Thread(new Runnable()
{
@Override
public void run()
{
while(true)
{
try
{
arrayList.add(new ServerSocket(9090).accept());
}
catch (IOException e)
{
throw new RuntimeException(e);
}
}
}
}).start();
new Thread(new Runnable()
{
@Override
public void run()
{
while(true)
{
if(arrayList.size() != 0)
{
for(int i = 0; i < arrayList.size(); i++)
{
String ip = arrayList.get(i).getInetAddress().getHostAddress();
try
{
int len;
byte[] bytes = new byte[10240];
while((len = arrayList.get(i).getInputStream().read()) != -1)
{
arrayList.get(i).getOutputStream().write(bytes,0,len);
}
}
catch (IOException e)
{
throw new RuntimeException(e);
}
System.out.println(ip);
}
}
if(arrayList.size() == 0)
{
System.out.println("No User");
}
}
}
}).start();
}
}文章来源地址https://www.toymoban.com/news/detail-606325.html
到了这里,关于计算机网络技术与JAVA网络编程手写Socket聊天室-----JAVA入门基础教程-----计算机网络经典的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!