javasocket客户端
Ⅰ java socket 客户端设置未接收到数据的超时问题
建议如果是长时间没有收到数据的话,或者是不想长时间等待的话,都可以通过设置超时时间来进行断开连接,之后继续处理逻辑(设置socket的setSoTimeout方法实现即可,时间单位是毫秒):
packagesocket;
importjava.io.ByteArrayOutputStream;
importjava.io.InputStream;
importjava.net.InetSocketAddress;
importjava.net.ServerSocket;
importjava.net.Socket;
importjava.net.SocketAddress;
importjava.net.SocketException;
importjava.net.SocketTimeoutException;
importjava.text.SimpleDateFormat;
importjava.util.Arrays;
importjava.util.Date;
publicclassSocketService{
publicstaticvoidmain(String[]args){
try{
SocketAddressaddress=newInetSocketAddress("192.168.9.155",3000);
//启动监听端口8001
ServerSocketss=newServerSocket();
ss.bind(address);
//接收请求
Sockets=ss.accept();
newThread(newT(s)).start();
}catch(Exceptione){
e.printStackTrace();
}
}
}
classTimplementsRunnable{
publicvoidrun(){
try{
System.out.println(socket.toString());
socket.setKeepAlive(true);
socket.setSoTimeout(5*1000);
String_pattern="yyyy-MM-ddHH:mm:ss";
SimpleDateFormatformat=newSimpleDateFormat(_pattern);
while(true){
System.out.println("开始:"+format.format(newDate()));
try{
InputStreamips=socket.getInputStream();
ByteArrayOutputStreambops=newByteArrayOutputStream();
intdata=-1;
while((data=ips.read())!=-1){
System.out.println(data);
bops.write(data);
}
System.out.println(Arrays.toString(bops.toByteArray()));
}catch(SocketTimeoutExceptione){
e.printStackTrace();
}catch(SocketExceptione){
e.printStackTrace();
}catch(Exceptione){
e.printStackTrace();
}
Thread.sleep(1000);
System.out.println(socket.isBound());//是否邦定
System.out.println(socket.isClosed());//是否关闭
System.out.println(socket.isConnected());//是否连接
System.out.println(socket.isInputShutdown());//是否关闭输入流
System.out.println(socket.isOutputShutdown());//是否关闭输出流
System.out.println("结束:"+format.format(newDate()));
}
}catch(Exceptione){
e.printStackTrace();
}
}
privateSocketsocket=null;
publicT(Socketsocket){
this.socket=socket;
}
publicSocketgetSocket(){
returnsocket;
}
publicvoidsetSocket(Socketsocket){
this.socket=socket;
}
}
Ⅱ socket java实现客户端多线程接受消息并发送消息给服务器,并发执行
客服端:
package MyKeFudaun;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.Socket;
import java.net.UnknownHostException;
public class KeFuDuan {
public static void main(String[] args) {
KeFuDuan kf = new KeFuDuan();
kf.start();
}
public void start(){
Socket sco;
String ss= "";
try {
sco = new Socket("127.0.0.1",8866);
KeFuanJie kf = new KeFuanJie(sco);
KeFuWuFasong kfs = new KeFuWuFasong(sco);
kf.start();
kfs.start();
//sco.close();
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
class KeFuanJie extends Thread{
Socket soc;
String ss;
BufferedReader br;
public KeFuanJie(Socket soc){
try {
this.soc = soc;
br = new BufferedReader(new InputStreamReader(soc.getInputStream()));
} catch (IOException e) {
e.printStackTrace();
}
}
//负责接受服务端来的信息
public void run(){
while(true){
//接受服务器端来的信息
try {
ss = br.readLine();
System.out.println("服务器---->客服端: "+ss);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
//向服务器发送东西
class KeFuWuFasong extends Thread{
Socket soc;
BufferedWriter bw;
BufferedReader brr;
public KeFuWuFasong(Socket soc){
this.soc = soc;
try {
brr =new BufferedReader(new InputStreamReader(System.in));
bw = new BufferedWriter(new OutputStreamWriter(soc.getOutputStream()));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void run(){
while(true){
//向服务器发送请求
try {
bw.write(brr.readLine());
bw.newLine();
bw.flush();// 或者用bw.close()
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
服务器端:
package MyKeFudaun;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.ServerSocket;
import java.net.Socket;
public class Server {
public static void main(String[] args) {
Server server = new Server();
server.start();
}
public void start(){
try { //服务器端打开端口
ServerSocket server = new ServerSocket(4499);
Socket socket = null;
ServerToClientThread stct = null;
while(true){
socket = server.accept(); //迎接(接收)客户端的Socket访问
stct = new ServerToClientThread(socket); //分配一个新线程负责和信赖的Socket沟通
stct.start();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
package MyKeFudaun;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.Socket;
public class ServerReceiveFromClient extends Thread{
Socket socket;
BufferedReader br;
String s;
public ServerReceiveFromClient(Socket socket){
this.socket = socket;
try {
br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
} catch (IOException e) {
e.printStackTrace();
}
}
public void run(){
while(true){
try {
s = br.readLine();
System.out.println(socket.getInetAddress().getHostAddress()+"发送了:"+s);
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
package MyKeFudaun;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.Socket;
public class ServerToClientThread extends Thread{
Socket socket;
BufferedReader br;
BufferedWriter bw;
String s;
//建立的同时,和客户端的Socket建立输入、输出流
public ServerToClientThread(Socket socket){
this.socket = socket;
try {
bw = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
} catch (IOException e) {
e.printStackTrace();
}
}
public void run(){
ServerReceiveFromClient srfc = new ServerReceiveFromClient(socket);
srfc.start();
while(true){
try {
bw.write("欢迎光临。");
bw.newLine();
bw.flush();
Thread.sleep(10*1000);
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
Ⅲ java socket 客户端是如何向服务器端发送消息的
服务器端和客户端都是通过SOCKET来进行通信的,首先产生一个 socket实例,通过这个实例,服务器端调用accept这个方法接收来自客户端发送的信息.但是在产生socket实例的时候必须初始化一个端口.用来负责接受客户端的请求!
客户端要给服务器发送消息也必须产生一个socket实例,初始化的时候必须指定服务器的IP地址,并且指定服务接收的端口号,这样客户端才能找到服务器要接收的地方,找到地方就可以发送过去了。和你写信一样。找到地址
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
PrintWriter out = new PrintWriter(socket.getOutputStream());
BufferedReader wt = new BufferedReader(new InputStreamReader(System.in));
这个只是用来获取一个从键盘的一个流.传送给服务器端的数据都是通过流来表示的。意思是是键盘输入的一个字节转化成字符流.并输出或者写入!
Ⅳ Java Socket中服务器断开后,客户端为什么没有抛异常
在客户端退出之后,会出现socket连接中断,此时是会抛出异常,在抛出异常时打印中断的客户端即可。
1. while(Binput.read()!= -1) 这种方法能判断出客户端输入是否为空,客户端断开能跳出死循环,但是得到的数据却从第二个开始了,所以这方法不适宜。
2. 在死循环中加入以下代码:
try {
socket.sendUrgentData(0);
} catch (IOException e) {
done= false; //如果抛出了异常,那么就是断开连接了 跳出无限循环
}