亚洲av成人无遮挡网站在线观看,少妇性bbb搡bbb爽爽爽,亚洲av日韩精品久久久久久,兔费看少妇性l交大片免费,无码少妇一区二区三区

  免費(fèi)注冊 查看新帖 |

Chinaunix

  平臺 論壇 博客 文庫
最近訪問板塊 發(fā)新帖
查看: 2795 | 回復(fù): 2
打印 上一主題 下一主題

c客戶端連接java服務(wù)端問題 [復(fù)制鏈接]

論壇徽章:
0
跳轉(zhuǎn)到指定樓層
1 [收藏(0)] [報(bào)告]
發(fā)表于 2005-08-10 19:12 |只看該作者 |倒序?yàn)g覽
c服務(wù)端程序:
  1. /*
  2. * copyright (C) 1986, 1988 by Larry McVoy.
  3. * MUST be distributed in source form only.
  4. */
  5. /* server.c 1.0 - main */
  6. # include        <stdio.h>;
  7. # include        <signal.h>;
  8. # include        <sys/types.h>;
  9. # include        <sys/socket.h>;
  10. # include        <sys/time.h>;
  11. # include        <netinet/in.h>;
  12. # include        <netdb.h>;
  13. #define        MYPORT        6300
  14. #define BACKLOG 10 /* 多少等待連接控制*/
  15. struct sockaddr_in osin,sin = { AF_INET };  /* the rest is null */

  16. main(argc, argv)
  17.         char** argv;
  18. {
  19.         char buf[128];
  20.         int seq, namelen, newsock, sock,netint,count;
  21.         if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
  22.         perror("socket");
  23.         exit(1);
  24.         }
  25.         sin.sin_port=htons(MYPORT);
  26.         //sin.sin_addr.s_addr = inet_addr("52.0.98.222");
  27.         sin.sin_addr.s_addr = INADDR_ANY;
  28.         bzero(&(sin.sin_zero),sizeof(sin.sin_zero));
  29.         if (bind(sock, (struct sockaddr *)&sin, sizeof(sin)) < 0) {
  30.         perror("bind");
  31.         exit(2);
  32.         }
  33.         namelen = sizeof(sin);
  34.         if (getsockname(sock, (struct sockaddr *)&sin,&namelen) < 0) {
  35.         perror("getsockname");
  36.         exit(3);
  37.         }
  38.         printf("SERVER:>; Server bound to port %u 0x%x\n", ntohs(sin.sin_port), sin.sin_port);
  39.         if (fork())
  40.         exit(0);
  41.          else printf("SERVER:>; Created a new process!\n");
  42.         if (listen(sock, BACKLOG) < 0) {
  43.         perror("listen");
  44.         exit(4);
  45.         } else printf("SERVER:>; listening...\n");
  46.         namelen = sizeof(sin);
  47.         while(1){
  48.                 if ((newsock = accept(sock, (struct sockaddr *)&osin,&namelen)) < 0) {
  49.                         perror("accept");
  50.                         continue;
  51.                 }else printf("SERVER:>;CLIENT IP:%s\n",inet_ntoa(osin.sin_addr));
  52.                 if (!fork()) { /* this is the child process */
  53.                         if (recv( newsock, buf, sizeof(buf), 0)<0) {
  54.                                 perror("recv");
  55.                                 continue;
  56.                         }else printf("SERVER:>; recv %s\n",buf);
  57.                         if (send(newsock, "Hello, world!\n", 14, 0) == -1)
  58.                                  perror("send");
  59.                         close(newsock);
  60.                         break;
  61.                 }

  62.         }
  63.         close(newsock); /* parent doesn't need this */
  64.         printf("SERVER:>; The server program is over now.\n");
  65. }
復(fù)制代碼


c客戶端程序:
  1. /*
  2. * copyright (C) 1986, 1988 by Larry McVoy.
  3. * MUST be distributed in source form only.
  4. */
  5. # include        <stdio.h>;
  6. # include        <signal.h>;
  7. # include        <errno.h>;
  8. # include        <sys/types.h>;
  9. # include        <sys/socket.h>;
  10. # include        <sys/time.h>;
  11. # include        <netinet/in.h>;
  12. # include        <netdb.h>;
  13. #define        MYPORT        6300

  14. extern errno;
  15. struct timeval zero = { 0, 0 };

  16. main(argc, argv)
  17.     char** argv;
  18. {
  19.     struct hostent* h;
  20.     struct sockaddr_in sin;
  21.     char buf[128];
  22.     char *sendmsg="999999999|1000|";
  23.     int i, sock;
  24.     int bytes_send;
  25.    

  26.     if (argc != 2) {
  27.         fprintf(stderr, "usage: %s remotehost\n", argv[0]);
  28.         exit(0);
  29.     }
  30.     if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
  31.         perror("socket");
  32.         exit(1);
  33.     }
  34.     if (!(h = gethostbyname(argv[1]))) {
  35.         perror(argv[1]);
  36.         exit(2);
  37.     }else printf("The host name is %s\n",h->;h_name);
  38.     bzero(&sin, sizeof(sin));
  39.     sin.sin_family = AF_INET;
  40.     bcopy(h->;h_addr, &sin.sin_addr, h->;h_length);
  41.     sin.sin_port = htons(MYPORT);
  42.     printf("sin.sin_family is %d:sin.sin_addr is %s:sin.sin_port is %d\n",sin.sin_family,inet_ntoa(sin.sin_addr),ntohs(sin.sin_port));
  43.     if (connect(sock, (struct sockaddr *)&sin, sizeof(sin)) < 0) {
  44.         perror("connect");
  45.     }else printf("connect to sock success.\n");
  46.     bytes_send=send(sock, sendmsg, strlen(sendmsg), 0);
  47.     if (bytes_send < 0) {
  48.         perror("send");
  49.     }else printf("send to sock :%s.\n", sendmsg);
  50. if (recv(sock, &buf,sizeof(buf),0) < 0) {
  51.         perror("recv");
  52.     }else printf("recv from sock :%s.\n", buf);
  53.     close(sock);
  54. }
復(fù)制代碼


java服務(wù)端程序:

  1. import java.io.*;
  2. import java.net.*;

  3. public class EchoServer  {

  4. static void doService(Socket clientSocket) {
  5.        try {
  6.       BufferedReader socIn = null;
  7.       socIn = new BufferedReader(
  8.        new InputStreamReader(clientSocket.getInputStream()));   
  9.       PrintStream socOut = new PrintStream(clientSocket.getOutputStream());
  10.       while (true) {
  11.         String line = socIn.readLine();
  12.         socOut.println(line);
  13.       }
  14.      } catch (Exception e) {
  15.         System.err.println("Error in EchoServer:" + e);
  16.         }
  17.        }
  18.   
  19.        public static void main(String args[]){
  20.         ServerSocket listenSocket;
  21.         
  22. try {
  23.   listenSocket = new ServerSocket(6300); //port
  24.   while (true) {
  25.    Socket clientSocket = listenSocket.accept();
  26.    doService(clientSocket);
  27.   }
  28.         } catch (Exception e) {
  29.             System.err.println("Error in EchoServer:" + e);
  30.         }
  31.       }
  32.   }
復(fù)制代碼


c語言客戶端call c語言服務(wù)端是好的
但是call java服務(wù)端就死在那里了。
人已累,身心疲憊,特求助,不勝感激。

論壇徽章:
0
2 [報(bào)告]
發(fā)表于 2005-08-10 20:09 |只看該作者

c客戶端連接java服務(wù)端問題

呵呵,經(jīng)高手指點(diǎn),問題原來在這一句:
char *sendmsg="999999999|1000|";
改為:
char *sendmsg="999999999|1000|\n";
就可以了。

論壇徽章:
0
3 [報(bào)告]
發(fā)表于 2005-08-10 23:02 |只看該作者

c客戶端連接java服務(wù)端問題

socIn.readLine
您需要登錄后才可以回帖 登錄 | 注冊

本版積分規(guī)則 發(fā)表回復(fù)

  

北京盛拓優(yōu)訊信息技術(shù)有限公司. 版權(quán)所有 京ICP備16024965號-6 北京市公安局海淀分局網(wǎng)監(jiān)中心備案編號:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年舉報(bào)專區(qū)
中國互聯(lián)網(wǎng)協(xié)會會員  聯(lián)系我們:huangweiwei@itpub.net
感謝所有關(guān)心和支持過ChinaUnix的朋友們 轉(zhuǎn)載本站內(nèi)容請注明原作者名及出處

清除 Cookies - ChinaUnix - Archiver - WAP - TOP