`

server + client by socket

阅读更多
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) {
}
}
}
}
}
分享到:
评论

相关推荐

    WebSocket.zip_Server_client_rights_web socket_websocket

    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 ...

    delphi socket call php socket 例子

    // 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 ...

    UDP-client-server.rar_The First_row socket

    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.

    Simple Client-server Interactions using C#

    Simple Client-server Interactions using C# By BobJanova C#封装的简单的客户机/服务器Socket通讯程序,非常棒!

    计算机网络第六版答案

    This document contains the solutions to review questions and problems for the 5th edition of Computer Networking: A Top-Down Approach by Jim Kurose and Keith Ross. These solutions are being made ...

    c# socket 多线程

    //The server reads the meassage sent by the Client ,converts it to upper case and sends it back to the client. //Lastly close all the streams. try { if (socketForClient.Connected) { ...

    DbY网络五子棋-netWuziqi-v1.2.exe [源码包另行上传;]

    DbY网络五子棋-netWuziqi-v1.2.exe 目的: Socket编程/网络编程训练; 特点: 双机联网对战+优化界面+背景音乐+倒计时+悔棋认输+聊天+帮助菜单; 编译环境: VC++6.0 (多文件.cpp+Socket/MFC库+对话框资源); C++写的网络...

    Socket TCP协议的服务类(20110304修正)

    /// /// By: Yi Dongliang /// Date:2010-04-23 /// Socket TCP协议的服务类。...SocketClient :客户端 源码有详细注释及说明。 如有任何BUG请反馈至 yidongliang123@163.com ,谢谢! www.bugucn.com

    DbY网络五子棋2合1迷你版v1.1@vs2019.rar [绿色免安装EXE+运行库+使用说明;源码另行打包;]

    DbY网络五子棋2合1迷你版v1.1--netWzq_mini v1.1@vs2019mfc.exe 目的: Socket编程/网络编程训练; 特点: 双机联网对战+单机人机对弈+超便携迷你界面(最小640x480)+提示音+悔棋认输+复盘+聊天+帮助菜单; 编译环境: vs...

    TcpServer 服务类

    /// /// By: Yi Dongliang /// Date:2010-04-23 /// Socket TCP协议的服务类。...SocketClient :客户端 源码有详细注释及说明。 如有任何BUG请反馈至 yidongliang123@163.com ,谢谢! www.bugucn.com

    qt socket 一对多-多线程

    接着刚才上传的qt socket one by one ,并程,多线程,最终完善了容错,和中英文,基本实现qt 局域网通信小程序。。。注意,阅读readme ,我编写的server 要用我的client 端接收(有设计字符格式问题)。。然后完事...

    IRC-chatSystem:linux socket网络编程 by c program,IRC-chatSystem

    将该程序放在linux系统的主文件夹中,直接终端输入./server ./client 即可运行。 具体使用查看使用说明书 源代码一份 client.c server.c (含user.ini) 技术说明文档一份 IRC软件技术报告.pdf 使用说明文档一份 IRC...

    linux 下 结合ftp 和部分telnet 功能的服务器源码

    # Super Ftp Server and Client for Wall-e # # # # SunMay30 2009 # # # ################################################################################ ////////\\\\\\\ ||Introduct || \\\\\...

    python 编写简单网页服务器的实例

    IDE:Pycharm sever.py #!/bin/python #-*- coding: UTF-8 -*- #文件名:server.py #create by wzh 2017/10/26 ...def handle_client(client_socket): """处理客户端连接请求""" request_data=client_socket.recv(10

    TypeScript类型定义DefinitelyTyped.zip

    Google API Client (by Frank M) Google App Engine Channel API (by vvakame) GoogleMaps (by Esben Nepper) Google Geolocation (by Vincent Bortone) Google Page Speed Online API (by Frank M) ...

    UNIX Network Programming Volume 1, Third Edition (Unix网络编程卷1第3版英文版)

    Roadmap to Client/Server Examples in the Text Section 1.7. OSI Model Section 1.8. BSD Networking History Section 1.9. Test Networks and Hosts Section 1.10. Unix Standards Section 1.11. 64-...

    Serial Port Server with sample code

    More jobs can be done by implementing the client ;; utilities. It is listening on `[Listen_Port]/Data' port. ;; ;;------------------------------------------------------------------------------ ;; Tip...

    hands-network-programming-c-optimized.rar

    By the end of this book, you’ll have experience of working with client-server applications, and be able to implement new network programs in C. The code in this book is compatible with the older C99 ...

Global site tag (gtag.js) - Google Analytics