Java 的通信编程,编程题(或问答),用 JAVA SOCKET 编程, 读服务器几个字符,再写入本地显示?
Server端
public class Server {
public static void main (String [] args){
new Server();
}
public Server(){
ServerSocket ss = null ;
Socket soc;
BufferedReader in = null;
try {
ss=new ServerSocket(12345);
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
while(true)
try {
soc = ss.accept();
System.out.println("有人进来了 ip为:"+soc.getInetAddress().getHostAddress()+" : "+soc.getPort());
in = new BufferedReader(new InputStreamReader(soc.getInputStream()));
System.out.println("Client send is "+in.readLine());
} catch (IOException e) {
if(ss!=null){
try {
System.out.println("服务端错误处理");
ss.close();
} catch (IOException e2) {
}
}
// TODO Auto-generated catch block
System.out.print("错误是"+e.getLocalizedMessage());
e.printStackTrace();
}
}
}
Client端
public class Client {
Socket socket;
public static void main(String[] args) {
// TODO Auto-generated method stub
new Client();
}
public Client(){
PrintWriter out = null;
BufferedReader in =null;
try {
socket = new Socket("127.0.0.1",12345);
System.out.println("Server Connect Success");
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
while(true){
try {
System.out.println("打字:");
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
out = new PrintWriter(socket.getOutputStream(),true);
out.println(br.readLine());
in = new BufferedReader(new InputStreamReader(System.in));
System.out.println("client in :" + in.readLine());
} catch (Exception e) {
try {
System.out.println("Client is wrong");
socket.close();
out.close();
in.close();
} catch (Exception e1e) {
}
}
}
}
}
分享到:
相关推荐
client_socket, addr = server_socket.accept() print(f'Connected by {addr}') # 这里处理与客户端的通信 client_socket.close() ``` 客户端的代码则会包含连接服务器、发送消息和接收消息的部分: ```python...
Based on previous implementations by Copyright 2010 Ben Swanson and Copyright 2010 Randall Brewer and Copyright 2010 Oliver Smith Some code and concept based off of Webduino library Copyright 2009 ...
在"udp p2p的server和client"这个程序中,我们可能涉及到以下几个关键知识点: 1. **UDP套接字编程**:在创建UDP服务器时,需要使用socket库创建一个UDP套接字,并将其绑定到特定的IP地址和端口上。服务器通过...
client_socket, client_address = server_socket.accept() print(f"Connected by {client_address}") # 交换数据 data = client_socket.recv(1024) print(f"Received: {data.decode()}") client_socket....
本例中,我们探讨的是如何使用Python的socket库创建一个简单的服务端(server.py)和客户端(client.py)来实现双向通信。 1. **Python socket基础知识**: - `socket`模块提供了低级网络通信接口,它是基于...
client_socket, addr = server_socket.accept() print(f"Connected by {addr}") message = client_socket.recv(1024).decode() print(f"Received: {message}") response = "Hello from Server!" client_...
client_socket, client_address = server_socket.accept() print(f"Connected by {client_address}") # 接收客户端发送的数据 data = client_socket.recv(1024).decode('utf-8') # 处理数据并发送响应 ...
client_socket, client_address = server_socket.accept() print(f"Connected by {client_address}") data = client_socket.recv(1024) print("Received:", data.decode()) client_socket.sendall(b"Hello,...
client_socket, client_address = server_socket.accept() print(f"Connected by {client_address}") ``` 3. **发送和接收数据(Send and Receive Data)**: - 一旦连接建立,服务器和客户端就可以通过各自的...
client_socket, addr = server_socket.accept() print('Connected by', addr) data = client_socket.recv(1024) print('Received:', data.decode()) client_socket.sendall(data.upper().encode()) client...
在Python编程中,`socket`模块是用于网络通信的基础接口,它可以实现客户端(client)与服务器端(server)之间的数据交换。在这个场景中,我们有`client.py`和`server.py`两个文件,分别代表了客户端和服务器端的...
client_socket, addr = server_socket.accept() print(f'Connected by {addr}') message = client_socket.recv(1024) print(f'Received: {message.decode()}') client_socket.send('Hello, Client!'.encode...
client_socket, addr = server_socket.accept() print(f"Connected by {addr}") request = client_socket.recv(1024).decode('utf-8') print("Request: ", request) response = "HTTP/1.1 200 OK\n\nHello,...
// create a copy, so $clients doesn't get modified by socket_select() $read = $clients ; // get a list of all the clients that have data to be read from // if there are no clients with data, go ...
Server application retrieves data through a socket connectionless over UDP. A client application is done by sending UDP-datagrams. IP-address and port of the remote recipient must zadavaёtsya user.
client_socket, addr = server_socket.accept() print(f'Connected by {addr}') while True: data = client_socket.recv(1024) if not data: break client_socket.send(data) client_socket.close() ...
client_socket.connect(server_address) message = 'Hello, Server!' client_socket.sendall(message.encode('utf-8')) response = client_socket.recv(1024) print('Received:', response.decode('utf-8')) ...
通过阅读和实践《Linux_Socket_Programming_By_Example》中的源码,你可以更深入地理解这些概念,并掌握Linux Socket编程的实际操作。记得结合`man`手册页学习相关的系统调用细节,这将有助于你在实际开发中灵活应用...
client_socket, client_address = server_socket.accept() print(f'Connected by {client_address}') # 循环读取客户端发送的数据 while True: data = client_socket.recv(1024) if not data: break ...
Simple Client-server Interactions using C# By BobJanova C#封装的简单的客户机/服务器Socket通讯程序,非常棒!