`
haoningabc
  • 浏览: 1476709 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

tcp/ip sockets 编程 java和c的对比

阅读更多
java的socket相对容易
server端:
1.建立ServerSocket(port);
2.获取客户端socket:Socket socket = server.accept();
3.读数据:Reader reader = new InputStreamReader(socket.getInputStream());//3
4.写数据:Writer writer = new OutputStreamWriter(socket.getOutputStream());//4
client端:
1.建立socket:Socket client = new Socket(host, port);//1
2.写数据:Writer writer = new OutputStreamWriter(client.getOutputStream());//2
3.读数据:Reader reader = new InputStreamReader(client.getInputStream());//3

而c的socket为:
server端:
1.建立socket:s=socket(AF_INET,SOCK_STREAM,0))
2.bind地址:bind(s,(struct sockaddr *)&my_addr,sizeof(struct sockaddr))
3.监听:listen(s,5);
4.获取客户端socket:cli=accept(s,(struct sockaddr *)&remote_addr,&sin_size))
5.向客户端写:len=send(cli,"welcome to my server\n",21,0);
6.从客户端读:len=recv(cli,buf,13,0);
client端:
1.建立socket:s=socket(AF_INET,SOCK_STREAM,0)),
2.链接server:connect(s,(struct sockaddr *)&remote_addr,sizeof(struct sockaddr))
3.从server读:len=recv(s,buf,BUFSIZ,0);
4.向server写len=send(s,"test3 message",13,0);



书的代码在
http://cs.ecs.baylor.edu/~donahoo/practical/CSockets2/

例子远吗在https://github.com/killinux/mysocket
参考
http://bbs.csdn.net/topics/340199431
http://haohaoxuexi.iteye.com/blog/1979837

先写段c的
client:
[root@VM_192_107_centos ipv4]# cat TCPEchoClient4.c 
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include "Practical.h"
int main(int argc, char *argv[]) {
    if (argc < 3 || argc > 4) // Test for correct number of arguments
        DieWithUserMessage("Parameter(s)",
                "<Server Address> <Echo Word> [<Server Port>]");
    char *servIP = argv[1];     // First arg: server IP address (dotted quad)
    char *echoString = argv[2]; // Second arg: string to echo
    // Third arg (optional): server port (numeric).  7 is well-known echo port
    in_port_t servPort = (argc == 4) ? atoi(argv[3]) : 7;
    // Create a reliable, stream socket using TCP
    int sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
    if (sock < 0)
        DieWithSystemMessage("socket() failed");
    // Construct the server address structure
    struct sockaddr_in servAddr;            // Server address
    memset(&servAddr, 0, sizeof(servAddr)); // Zero out structure
    servAddr.sin_family = AF_INET;          // IPv4 address family
    // Convert address
    int rtnVal = inet_pton(AF_INET, servIP, &servAddr.sin_addr.s_addr);
    if (rtnVal == 0)
        DieWithUserMessage("inet_pton() failed", "invalid address string");
    else if (rtnVal < 0)
        DieWithSystemMessage("inet_pton() failed");
    servAddr.sin_port = htons(servPort);    // Server port
    // Establish the connection to the echo server
    if (connect(sock, (struct sockaddr *) &servAddr, sizeof(servAddr)) < 0)
        DieWithSystemMessage("connect() failed");
    size_t echoStringLen = strlen(echoString); // Determine input length
    // Send the string to the server
    ssize_t numBytes = send(sock, echoString, echoStringLen, 0);
    if (numBytes < 0)
        DieWithSystemMessage("send() failed");
    else if (numBytes != echoStringLen)
        DieWithUserMessage("send()", "sent unexpected number of bytes");
    // Receive the same string back from the server
    unsigned int totalBytesRcvd = 0; // Count of total bytes received
    fputs("Received: ", stdout);     // Setup to print the echoed string
    while (totalBytesRcvd < echoStringLen) {
        char buffer[BUFSIZE]; // I/O buffer
        /* Receive up to the buffer size (minus 1 to leave space for
           a null terminator) bytes from the sender */
        numBytes = recv(sock, buffer, BUFSIZE - 1, 0);
        if (numBytes < 0)
            DieWithSystemMessage("recv() failed");
        else if (numBytes == 0)
            DieWithUserMessage("recv()", "connection closed prematurely");
        totalBytesRcvd += numBytes; // Keep tally of total bytes
        buffer[numBytes] = '\0';    // Terminate the string!
        fputs(buffer, stdout);      // Print the echo buffer
    }
    fputc('\n', stdout); // Print a final linefeed
    close(sock);
    exit(0);
}

server:
[root@VM_192_107_centos ipv4]# cat TCPEchoServer4.c 
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include "Practical.h"
static const int MAXPENDING = 5; // Maximum outstanding connection requests
int main(int argc, char *argv[]) {
    if (argc != 2) // Test for correct number of arguments
        DieWithUserMessage("Parameter(s)", "<Server Port>");
    in_port_t servPort = atoi(argv[1]); // First arg:  local port
    // Create socket for incoming connections
    int servSock; // Socket descriptor for server
    if ((servSock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0)
        DieWithSystemMessage("socket() failed");
    // Construct local address structure
    struct sockaddr_in servAddr;                  // Local address
    memset(&servAddr, 0, sizeof(servAddr));       // Zero out structure
    servAddr.sin_family = AF_INET;                // IPv4 address family
    servAddr.sin_addr.s_addr = htonl(INADDR_ANY); // Any incoming interface
    servAddr.sin_port = htons(servPort);          // Local port
    // Bind to the local address
    if (bind(servSock, (struct sockaddr*) &servAddr, sizeof(servAddr)) < 0)
        DieWithSystemMessage("bind() failed");
    // Mark the socket so it will listen for incoming connections
    if (listen(servSock, MAXPENDING) < 0)
        DieWithSystemMessage("listen() failed");
    for (;;) { // Run forever
        struct sockaddr_in clntAddr; // Client address
        // Set length of client address structure (in-out parameter)
        socklen_t clntAddrLen = sizeof(clntAddr);
        // Wait for a client to connect
        int clntSock = accept(servSock, (struct sockaddr *) &clntAddr, &clntAddrLen);
        if (clntSock < 0)
            DieWithSystemMessage("accept() failed");
        // clntSock is connected to a client!
        char clntName[INET_ADDRSTRLEN]; // String to contain client address
        if (inet_ntop(AF_INET, &clntAddr.sin_addr.s_addr, clntName,
                    sizeof(clntName)) != NULL)
            printf("Handling client %s/%d\n", clntName, ntohs(clntAddr.sin_port));
        else
            puts("Unable to get client address");
        HandleTCPClient(clntSock);
    }
    // NOT REACHED
}

运行用
gcc -g -std=gnu99 TCPEchoServer4.c ../socket/DieWithMessage.c ../socket/TCPServerUtility.c ../socket/AddressUtility.c -o TCPEchoServer4
gcc -g TCPEchoClient4.c ../socket/DieWithMessage.c -o TCPEchoClient4

其中HandleTCPClient 为
void HandleTCPClient(int clntSocket) {
    char buffer[BUFSIZE]; // Buffer for echo string
    // Receive message from client
    ssize_t numBytesRcvd = recv(clntSocket, buffer, BUFSIZE, 0);
    if (numBytesRcvd < 0)
        DieWithSystemMessage("recv() failed");
    // Send received string and receive again until end of stream
    ssize_t numBytesSent_begin = send(clntSocket, "this server:",13 , 0);
    while (numBytesRcvd > 0) { // 0 indicates end of stream
        // Echo message back to client
        ssize_t numBytesSent = send(clntSocket, buffer, numBytesRcvd, 0);
        if (numBytesSent < 0)
            DieWithSystemMessage("send() failed");
        else if (numBytesSent != numBytesRcvd)
            DieWithUserMessage("send()", "sent unexpected number of bytes");

        // See if there is more data to receive
        numBytesRcvd = recv(clntSocket, buffer, BUFSIZE, 0);
        if (numBytesRcvd < 0)
            DieWithSystemMessage("recv() failed");
    }

    close(clntSocket); // Close client socket
}

----------------------------------------------------
再写一段java的 server
package com.hao;

import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.Reader;
import java.io.Writer;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.SocketAddress;

public class Server {

	public static void main(String args[]) throws IOException {
		int port = 8080;
		ServerSocket server = new ServerSocket(port);
		while (true) {
			Socket socket = server.accept();
			Reader reader = new InputStreamReader(socket.getInputStream());
			SocketAddress sa = socket.getRemoteSocketAddress();
			System.out.println(sa.toString());
			char chars[] = new char[64];
			int len;
			StringBuilder sb = new StringBuilder();
			String temp;
			int index;
			while ((len = reader.read(chars)) != -1) {
				temp = new String(chars, 0, len);
				if ((index = temp.indexOf("eof")) != -1) {
					sb.append(temp.substring(0, index));
					break;
				}
				sb.append(temp);
			}
			System.out.println("from client: " + sb);
			Writer writer = new OutputStreamWriter(socket.getOutputStream());
			writer.write("Hello Client.");
			writer.flush();
			writer.close();
			reader.close();
			socket.close();
		}
	}
}


client:

import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.Reader;
import java.io.Writer;
import java.net.Socket;

public class Client {
	public static void main(String args[]) throws Exception {
		String host = "127.0.0.1";//"182.254.155.153";
		int port = 8080;
		Socket client = new Socket(host, port);
		Writer writer = new OutputStreamWriter(client.getOutputStream());
		writer.write("Hello Server hah.");
		writer.write("eof");
		writer.flush();
		Reader reader = new InputStreamReader(client.getInputStream());
		char chars[] = new char[64];
		int len;
		StringBuffer sb = new StringBuffer();
		String temp;
		int index;
		while ((len = reader.read(chars)) != -1) {
			temp = new String(chars, 0, len);
			if ((index = temp.indexOf("eof")) != -1) {
				sb.append(temp.substring(0, index));
				break;
			}
			sb.append(new String(chars, 0, len));
		}
		System.out.println("from server: " + sb);
		writer.close();
		reader.close();
		client.close();
	}
}


----------------------------------------------
再来一个长连接的:
package com.hao.changlianjie;

import java.io.Serializable;
import java.text.SimpleDateFormat;
import java.util.Date;

public class KeepAlive implements Serializable{
	 
    private static final long serialVersionUID = -2813120366138988480L;
 
    @Override
    public String toString() {
        return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date())+"\t维持连接包";
    }
 
}



client
package com.hao.changlianjie;

import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.concurrent.ConcurrentHashMap;

public class Client {

    /**
     * 处理服务端发回的对象,可实现该接口。
     */
    public static interface ObjectAction{
        void doAction(Object obj,Client client);
    }
    public static final class DefaultObjectAction implements ObjectAction{
        public void doAction(Object obj,Client client) {
            System.out.println("处理:\t"+obj.toString());
        }
    }
    public static void main(String[] args) throws UnknownHostException, IOException {
        String serverIp = "127.0.0.1";
        int port = 65432;
        Client client = new Client(serverIp,port);
        client.start();
    }
     
    private String serverIp;
    private int port;
    private Socket socket;
    private boolean running=false;
    private long lastSendTime;
    private ConcurrentHashMap<Class, ObjectAction> actionMapping = new ConcurrentHashMap<Class,ObjectAction>();
     
    public Client(String serverIp, int port) {
        this.serverIp=serverIp;this.port=port;
    }
     
    public void start() throws UnknownHostException, IOException {
        if(running)return;
        socket = new Socket(serverIp,port);
        System.out.println("本地端口:"+socket.getLocalPort());
        lastSendTime=System.currentTimeMillis();
        running=true;
        new Thread(new KeepAliveWatchDog()).start();
        new Thread(new ReceiveWatchDog()).start();
    }
     
    public void stop(){
        if(running)running=false;
    }
     
    /**
     * 添加接收对象的处理对象。
     * @param cls 待处理的对象,其所属的类。
     * @param action 处理过程对象。
     */
    public void addActionMap(Class<Object> cls,ObjectAction action){
        actionMapping.put(cls, action);
    }
 
    public void sendObject(Object obj) throws IOException {
        ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream());
        oos.writeObject(obj);
        System.out.println("发送:\t"+obj);
        oos.flush();
    }
     
    class KeepAliveWatchDog implements Runnable{
        long checkDelay = 10;
        long keepAliveDelay = 2000;
        public void run() {
            while(running){
                if(System.currentTimeMillis()-lastSendTime>keepAliveDelay){
                    try {
                        Client.this.sendObject(new KeepAlive());
                    } catch (IOException e) {
                        e.printStackTrace();
                        Client.this.stop();
                    }
                    lastSendTime = System.currentTimeMillis();
                }else{
                    try {
                        Thread.sleep(checkDelay);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                        Client.this.stop();
                    }
                }
            }
        }
    }
     
    class ReceiveWatchDog implements Runnable{
        public void run() {
            while(running){
                try {
                    InputStream in = socket.getInputStream();
                    if(in.available()>0){
                        ObjectInputStream ois = new ObjectInputStream(in);
                        Object obj = ois.readObject();
                        System.out.println("接收:\t"+obj);
                        ObjectAction oa = actionMapping.get(obj.getClass());
                        oa = oa==null?new DefaultObjectAction():oa;
                        oa.doAction(obj, Client.this);
                    }else{
                        Thread.sleep(10);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                    Client.this.stop();
                } 
            }
        }
    }
}



server:
package com.hao.changlianjie;

import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.concurrent.ConcurrentHashMap;

public class Server {
	/**
     * 要处理客户端发来的对象,并返回一个对象,可实现该接口。
     */
    public interface ObjectAction{
        Object doAction(Object rev);
    }
     
    public static final class DefaultObjectAction implements ObjectAction{
        public Object doAction(Object rev) {
            System.out.println("处理并返回:"+rev);
            return rev;
        }
    }
     
    public static void main(String[] args) {
        int port = 65432;
        Server server = new Server(port);
        server.start();
    }
     
    private int port;
    private volatile boolean running=false;
    private long receiveTimeDelay=3000;
    private ConcurrentHashMap<Class, ObjectAction> actionMapping = new ConcurrentHashMap<Class,ObjectAction>();
    private Thread connWatchDog;
     
    public Server(int port) {
        this.port = port;
    }
 
    public void start(){
        if(running)return;
        running=true;
        connWatchDog = new Thread(new ConnWatchDog());
        connWatchDog.start();
    }
     
    @SuppressWarnings("deprecation")
    public void stop(){
        if(running)running=false;
        if(connWatchDog!=null)connWatchDog.stop();
    }
     
    public void addActionMap(Class<Object> cls,ObjectAction action){
        actionMapping.put(cls, action);
    }
     
    class ConnWatchDog implements Runnable{
        public void run(){
            try {
                ServerSocket ss = new ServerSocket(port,5);
                while(running){
                    Socket s = ss.accept();
                    new Thread(new SocketAction(s)).start();
                }
            } catch (IOException e) {
                e.printStackTrace();
                Server.this.stop();
            }
             
        }
    }
     
    class SocketAction implements Runnable{
        Socket s;
        boolean run=true;
        long lastReceiveTime = System.currentTimeMillis();
        public SocketAction(Socket s) {
            this.s = s;
        }
        public void run() {
            while(running && run){
                if(System.currentTimeMillis()-lastReceiveTime>receiveTimeDelay){
                    overThis();
                }else{
                    try {
                        InputStream in = s.getInputStream();
                        if(in.available()>0){
                            ObjectInputStream ois = new ObjectInputStream(in);
                            Object obj = ois.readObject();
                            lastReceiveTime = System.currentTimeMillis();
                            System.out.println("接收:\t"+obj);
                            ObjectAction oa = actionMapping.get(obj.getClass());
                            oa = oa==null?new DefaultObjectAction():oa;
                            Object out = oa.doAction(obj);
                            if(out!=null){
                                ObjectOutputStream oos = new ObjectOutputStream(s.getOutputStream());
                                oos.writeObject(out);
                                oos.flush();
                            }
                        }else{
                            Thread.sleep(10);
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                        overThis();
                    } 
                }
            }
        }
         
        private void overThis() {
            if(run)run=false;
            if(s!=null){
                try {
                    s.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            System.out.println("关闭:"+s.getRemoteSocketAddress());
        }
         
    }
     
}

分享到:
评论

相关推荐

    TCP_IP_Sockets_in_Java

    《TCP/IP Sockets in Java》是一本专注于Java网络编程的深度指南,主要涵盖了TCP/IP通信协议栈以及如何在Java环境中实现Socket编程。本书的核心内容包括TCP/IP基础知识、Java Socket API的使用、网络数据传输以及多...

    TCP/IP sockets in java pratical guide for programmers second edition

    《TCP/IP Sockets in Java: 实用指南(第二版)》是一本专门为程序员设计的书,深入探讨了如何在Java环境中实现和操作TCP/IP套接字。本书由肯尼思·L·卡尔弗特(Kenneth L. Calvert)和迈克尔·J·多纳霍(Michael J. ...

    TCP.IP.Sockets.in.Java.2nd.Edition.pdf (TCP/IP Socket 编程)

    7. 其他编程语言和平台的网络编程:书名提及的其他书籍和系列,如《TCP/IP Sockets in C#》和《TCP/IPSockets in C》,表明读者可以将从本书获得的知识应用于其他编程语言和平台上的网络编程。 8. 出版和版权信息:...

    TCP/IP Socket in C/Java

    两本TCP/IP socket编程书,分别是C语言和Java语言版的。都是文字版的pdf文档。 1. TCP/IP Sockets in Java (2th Edtion) 2. TCP/IP Sockets in C *Series Editor: Michael J. Donahoo

    TCPIP Sockets in Java Practical Guide for Programmers

    《TCP/IP Sockets in Java 实践程序员指南》这本书专注于Java平台上的网络编程,特别是TCP/IP套接字的应用。TCP/IP是互联网通信的基础,而Java中的套接字(Sockets)接口为开发者提供了实现网络通信的工具。这本书...

    TCP-IP Sockets in Java, Second Edition- Practical Guide for Prog

    本书提供了丰富的实践案例,帮助读者更好地理解如何在实际项目中应用TCP/IP Sockets编程技术: - **聊天室应用程序**:演示如何使用Socket创建一个简单的聊天室。 - **文件传输服务**:展示如何使用Socket进行文件...

    TCPIP.Sockets.in.Java.Practical.Guide.for.Programmers.2nd.Edition

    这本书主要围绕Java中的TCP/IP编程展开,通过实例和详细的讲解帮助读者掌握网络编程的核心概念和技术。内容涵盖了TCP/IP协议的基本原理、Socket编程的基础知识以及如何在Java中实现这些技术的具体方法。 1. **基础...

    java基于TCP/IP的聊天室

    总的来说,创建一个Java基于TCP/IP的聊天室涉及到网络编程、多线程、数据传输安全以及用户界面设计等多个方面的知识。通过实践这个项目,开发者不仅可以深入理解TCP/IP协议,还能提升在并发处理、网络通信和GUI开发...

    TCP.IP.Sockets.in.Java.2nd.Edition

    《TCP/IP Sockets in Java: Practical Guide for Programmers》第二版是一本深入介绍如何在Java环境中使用TCP/IP套接字进行网络编程的专业书籍。本书由Kenneth L. Calvert与Michael J. Donahoo共同编写,并于2008年...

    Java TCP IP Socket编程(原书第2版)

    《Java TCP/IP Socket编程》(原书第2版)是一本深入探讨Java中Socket编程的专著,旨在帮助读者理解并掌握如何在Java环境中利用TCP/IP协议进行网络通信。本书不仅详细介绍了Socket的工作原理,还提供了丰富的示例...

    Java_TCPIP_Socket编程(中+英两本)

    本资源包含了两本书籍,一本是英文版的《TCP-IP Sockets in Java, 2nd Edition》(java网络编程外文教材),另一本是中文版的《Java TCP/IP Socket编程》,旨在帮助开发者深入理解和应用Java的Socket编程技术。...

    tcp\ip socket C语言编程

    根据提供的文件信息,我们可以推断出此文档主要围绕TCP/IP Sockets编程展开,并且特别强调了在C语言环境中的应用。尽管文档标题和描述都指向了C语言编程,但文件内容却提到了一系列关于不同技术(如Java、C#等)的...

    TCPIP.Sockets.in.Java.Second.Edition

    ### 关于《TCP/IP Sockets in Java 第二版》的关键知识点 #### 一、书籍概述与定位 《TCP/IP Sockets in Java 第二版》是一本面向程序员的实用指南,由Kenneth L. Calvert(肯尼斯·L·卡尔弗特)与Michael J. ...

    Android与C#Tcp/Ip通信

    在IT行业中,Android和C#之间的TCP/IP通信是一项基础但重要的技术,特别是在构建跨平台应用时。本主题将深入探讨如何使Android客户端与C#服务器端实现高效且可靠的16进制数据传输。 首先,我们要了解TCP/IP协议。...

    TCPIP Sockets in C#

    从提供的部分内容中我们可以看到,该系列还包括了其他有关Java和网络编程的书籍,例如《TCP/IP Sockets in Java》和《JDBC: Practical Guide for Java Programmers》。这表明该系列书籍专注于为程序员提供网络编程...

    利用属性页和套接字类以及TCP/IP协议实现网络通信

    本文将深入探讨如何利用属性页、套接字类以及TCP/IP协议来实现客户端和服务器端之间的有效通信。我们将详细介绍这些概念,以及它们如何协同工作以完成数据交换。 首先,属性页是一种用户界面元素,通常用于设置或...

Global site tag (gtag.js) - Google Analytics