1.java socket���Դ��
2.用Java Socket编程,编程编程实现简单的源码Echo功能
3.java socketç½ç»ç¼ç¨
4.编写一个简单的TCP通信程序。服务器发送“你好我是编程编程aptget源码安装服务器”,客户端接收该信息并显示在屏幕上。源码用Java写
java socket���Դ��
//客户端
public static void main(String[] args) throws Exception
{
Socket socket = new Socket(".0.0.1",编程编程仿代驾源码 );
OutputStream os = socket.getOutputStream();
os.write("hello world".getBytes());
os.close();//客户端使用完流之后记得要关闭!!源码棋牌真人源码搭建
}
//服务端
public static void main(String[] args) throws Exception
{
ServerSocket ss = new ServerSocket();
Socket socket = ss.accept();
InputStream is = socket.getInputStream();
byte[] buffer = new byte[];
int length = 0;
while (-1 != (length = is.read(buffer,编程编程 0, buffer.length)))//这句错了,是源码不等于!
{
String str = new String(buffer,编程编程 0, length);
System.out.println("welcome "+str);
}
}
用Java Socket编程,实现简单的源码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
编写一个简单的TCP通信程序。服务器发送“你好我是编程编程服务器”,客户端接收该信息并显示在屏幕上。源码用Java写
1、编程编程by 烟漫源码服务器端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("你好我是源码服务器");
} 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",编程编程android蓝牙配对源码 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) { }
}
}
}
}