免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2767 | 回复: 2
打印 上一主题 下一主题

c客户端连接java服务端问题 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2005-08-10 19:12 |只看该作者 |倒序浏览
c服务端程序:
  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. }
复制代码


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. }
复制代码


java服务端程序:

  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.   }
复制代码


c语言客户端call c语言服务端是好的
但是call java服务端就死在那里了。
人已累,身心疲惫,特求助,不胜感激。

论坛徽章:
0
2 [报告]
发表于 2005-08-10 20:09 |只看该作者

c客户端连接java服务端问题

呵呵,经高手指点,问题原来在这一句:
char *sendmsg="999999999|1000|";
改为:
char *sendmsg="999999999|1000|\n";
就可以了。

论坛徽章:
0
3 [报告]
发表于 2005-08-10 23:02 |只看该作者

c客户端连接java服务端问题

socIn.readLine
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP