1.java.net.socket Դ??
2.java.net.SocketException: Connection reset
3.用Java Socket编程,实现简单的如何上传源码Echo功能
4.java socketç½ç»ç¼ç¨
java.net.socket Դ??
1、服务器端import java.io.DataOutputStream;import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
public class SocketServer {
private static final int PORT = ;
public static void main(String[] args) {
ServerSocket server = null;
try {
server = new ServerSocket(PORT);
while (true) {
Socket client = server.accept();
new Thread(new Server(client)).start();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
class Server implements Runnable {
private Socket client;
public Server(Socket client) {
this.client = client;
}
public void run() {
DataOutputStream output = null;
try {
output = new DataOutputStream(client.getOutputStream());
output.writeUTF("你好我是wp论坛源码服务器");
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (output != null) output.close();
output = null;
} catch (IOException e) { }
}
}
}
2、客户端
import java.io.DataInputStream;import java.io.IOException;
import java.net.Socket;
import java.net.UnknownHostException;
public class Client extends Socket {
private static final int PORT = ;
public static void main(String[] args) {
Socket socket = null;
try {
socket = new Socket(".0.0.1",画线工具源码 PORT);
DataInputStream in = new DataInputStream(socket.getInputStream());
String res = in.readUTF();
System.out.println(res);
if (in != null) in.close();
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (socket != null) {
try {
socket.close();
} catch (IOException e) { }
}
}
}
}
java.net.SocketException: Connection reset
è¿æ¥éç½®
Kæä¸åä¸JAVAæå ³çè¿ç¨
é代ç ï¼
taskkill /im java.exe /f
taskkill /im javaw.exe /f
æè¿ä¸¤æ®µåå«Copyå°è¿è¡é æ²å车
éå¯æå¡å¨ å°±å¯ä»¥äº
用Java Socket编程,实现简单的联系qq源码Echo功能
// 服务器
import java.io.*;
import java.net.*;
public class MyServer {
public static void main(String[] args) throws IOException{
ServerSocket server=new ServerSocket();
while (true) {
Socket client=server.accept();
BufferedReader in=new BufferedReader(new InputStreamReader(client.getInputStream()));
PrintWriter out=new PrintWriter(client.getOutputStream());
while(true){
String str=in.readLine();
System.out.println(str);
out.println("has receive....");
out.flush();
if(str.equals("exit"))
break;
}
client.close();
}
}
}
// 客户端
import java.net.*;
import java.io.*;
public class Client{
static Socket server;
public static void main(String[] args)throws Exception{
server=new Socket(InetAddress.getLocalHost(),);
BufferedReader in=new BufferedReader(new InputStreamReader(server.getInputStream()));
PrintWriter out=new PrintWriter(server.getOutputStream());
BufferedReader wt=new BufferedReader(new InputStreamReader(System.in));
while(true){
String str=wt.readLine();
out.println(str);
out.flush();
if(str.equals("end")){
break;
}
System.out.println(in.readLine());
}
server.close();
}
}
java socketç½ç»ç¼ç¨
//==============Server.java=================//
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
public class Server {
public static void main(String[] args) throws IOException {
ServerSocket s = new ServerSocket();
System.out.println("æå¡å¨å°±ç»ª,请å¯å¨å®¢æ·ç«¯.");
Socket so = s.accept();
byte[] buff = new byte[];
int read = so.getInputStream().read(buff);
String[] abc=new String(buff,0,read).split("\\D+");
int a = Integer.parseInt(abc[0]);
int b = Integer.parseInt(abc[1]);
int c = Integer.parseInt(abc[2]);
if(!cbt(a,b,c))
so.getOutputStream().write("è¾å ¥çæ°æ®æ æ³ç»æä¸è§å½¢.".getBytes());
else
so.getOutputStream().write(getArea(a,b,c).getBytes());
so.getOutputStream().flush();
so.close();
s.close();
}
private static String getArea(int a, int b, int c) {
float s = (a+b+c)/2f;
return "é¢ç§¯: "+Math.sqrt(s*(s-a)*(s-b)*(s-c));
}
private static boolean cbt(int a, int b, int c) {
return a>0&&b>0&&c>0&&a+b>c&&b+c>a&&a+c>b;
}
}
//=================Client.java======================//
import java.io.IOException;
import java.net.Socket;
import java.net.UnknownHostException;
public class Client {
public static void main(String[] args) throws UnknownHostException, IOException {
System.out.println("è¾å ¥ä¸è§å½¢çä¸è¾¹å¹¶ç¨éå·éå¼,å¦: (3,4,5) ");
byte[] buff=new byte[];
int r = System.in.read(buff);
String ipaddr = "localhost";//æ ¹æ®æ åµæ¹å,å¨æ¬æºè°è¯å°±ä¸æ¹äº
Socket so = new Socket(ipaddr,);
so.getOutputStream().write(new String(buff,0,r).getBytes());
r = so.getInputStream().read(buff);
so.close();
String rs = new String(buff,0,r);
System.out.println(rs);
}
}
//å å¯å¨Server,åå¯å¨Client
2024-11-06 13:35
2024-11-06 12:14
2024-11-06 12:07
2024-11-06 11:49
2024-11-06 11:04
2024-11-06 11:01