1.java socketç½ç»ç¼ç¨
2.用Java Socket编程,类源实现简单的码j码分销平台免费源码Echo功能
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
用Java Socket编程,实现简单的类源源码dnf源码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();
}
}