在java中交际用C客户端TCP服务器 [英] TCP server in C client in java comunication
问题描述
我在c和客户端的Java服务器,但它不工作。
1.运行服务器,服务器等待客户端
2.运行客户端 - 客户端发送字符串
3.服务器获取冷杉性格和增加它发送给客户端//我想到这一点,但
服务器获取一些错误的字符
4.client得到的性格,写在屏幕//想到这个,但是客户端会失败,此
例外:
异常线程main产生java.net.SocketException:连接重置
在java.net.SocketInputStream.read(SocketInputStream.java:168)
在java.io.ObjectInputStream中的$ PeekInputStream.read(ObjectInputStream.java:2266)
在java.io.ObjectInputStream中的$ PeekInputStream.readFully(ObjectInputStream.java:2279)
在java.io.ObjectInputStream中的$ BlockDataInputStream.readShort(ObjectInputStream.java:2750)
在java.io.ObjectInputStream.readStreamHeader(ObjectInputStream.java:780)
在java.io.ObjectInputStream中的<&初始化GT;(ObjectInputStream.java:280)
在clientTCP.main(clientTCP.java:19)
5)客户端应该结束,服务器继续运行//但客户走下previous异常后
有谁知道在哪里可能是问题?服务器,客户端使用TCP协议。
服务器:
的#include< SYS / types.h中>
#包括LT&; SYS / socket.h中>
#包括LT&;&stdio.h中GT;
#包括LT&; SYS / un.h>
#包括LT&;&unistd.h中GT;
#包括LT&; netinet / in.h中>
#包括LT&;&unistd.h中GT;
#包括LT&;&signal.h中GT;
#包括LT&;&stdlib.h中GT;诠释的main()
{
INT server_sockfd,client_sockfd;
INT server_len,client_len;
结构SOCKADDR_IN服务器地址;
结构SOCKADDR_IN CLIENT_ADDRESS;
server_sockfd =插座(AF_INET,SOCK_STREAM,0);
server_address.sin_family = AF_INET;
server_address.sin_addr.s_addr = inet_addr(127.0.0.1);
server_address.sin_port = htons(10000); server_len = sizeof的(服务器地址); 如果(绑定(server_sockfd,(结构sockaddr *)及!SERVER_ADDRESS,server_len)= 0)
{
PERROR(哎呀:服务器TCP单);
出口(1);
} 听(server_sockfd,5); 信号(SIGCHLD,SIG_IGN); 而(1)
{
焦炭CH;
的printf(服务器等待...... \\ n); client_len = sizeof的(CLIENT_ADDRESS);
client_sockfd =接受(server_sockfd,(结构sockaddr *)及CLIENT_ADDRESS,&安培; client_len); 的printf(客户端连接\\ n); 如果(叉()== 0)
{
读(client_sockfd,&放大器; CH,1);
的printf(客户端发送=%C \\ N,CH); CH ++; 睡眠(5); 的printf(服务器发送=%C \\ N,CH);
写(client_sockfd,&安培; CH,1);
关闭(client_sockfd);
出口(0);
}
其他
关闭(client_sockfd); }
}
客户端在Java中:
进口java.io. *;
进口java.net *。类clientTCP
{
公共静态无效的主要(字符串的argv [])抛出异常
{
串句;
串modifiedSentence;
Socket套接字=新的Socket(127.0.0.1,10000);
InetAddress类adresa = socket.getInetAddress(); //地址
System.out.print(连接上:+ adresa.getHostAddress()+与主机名:+ adresa.getHostName()+\\ n);
ObjectOutputStream的OOS =新的ObjectOutputStream(socket.getOutputStream());
oos.writeObject(HalloXXXX);
ObjectInputStream的OIS =新的ObjectInputStream(socket.getInputStream());
字符串消息=(字符串)ois.readObject();
的System.out.println(收到消息:+消息);
ois.close();
oos.close();
socket.close();
}
}
您只能使用ObjectOutputStream的与其他Java程序也使用ObjectInputStream的阅读。这是很难想象一个C语言程序支持Java的序列化格式。
如果你想写的文字,我建议你使用的PrintWriter /或的BufferedReader一个更好的解决方案是使用的BufferedOutputStream,做你自己的编码(所以你看所有的错误。
I have server in c and client in java but it doesn't work.
1. run server, server wait for clients
2. run client - client sends string
3. server get the firs character and increment it and send to client //i expect this but
server gets some wrong char
4.client get character and write it on screen //expect this, but client fail with this
exception:
Exception in thread "main" java.net.SocketException: Connection reset
at java.net.SocketInputStream.read(SocketInputStream.java:168)
at java.io.ObjectInputStream$PeekInputStream.read(ObjectInputStream.java:2266)
at java.io.ObjectInputStream$PeekInputStream.readFully(ObjectInputStream.java:2279)
at java.io.ObjectInputStream$BlockDataInputStream.readShort(ObjectInputStream.java:2750)
at java.io.ObjectInputStream.readStreamHeader(ObjectInputStream.java:780)
at java.io.ObjectInputStream.<init>(ObjectInputStream.java:280)
at clientTCP.main(clientTCP.java:19)
5) client should end and server continue running // but client went down after the previous exception
Does anybody know where could be the problem? server, client uses TCP protocol.
server:
#include <sys/types.h>
#include <sys/socket.h>
#include <stdio.h>
#include <sys/un.h>
#include <unistd.h>
#include <netinet/in.h>
#include <unistd.h>
#include <signal.h>
#include <stdlib.h>
int main()
{
int server_sockfd, client_sockfd;
int server_len, client_len;
struct sockaddr_in server_address;
struct sockaddr_in client_address;
server_sockfd = socket( AF_INET, SOCK_STREAM, 0 );
server_address.sin_family = AF_INET;
server_address.sin_addr.s_addr = inet_addr( "127.0.0.1" );
server_address.sin_port = htons( 10000 );
server_len = sizeof( server_address );
if( bind( server_sockfd, ( struct sockaddr *)&server_address, server_len ) != 0 )
{
perror("oops: server-tcp-single");
exit( 1 );
}
listen( server_sockfd, 5 );
signal( SIGCHLD, SIG_IGN );
while( 1 )
{
char ch;
printf( "server wait...\n" );
client_len = sizeof( client_address );
client_sockfd = accept( server_sockfd, ( struct sockaddr *)&client_address, &client_len );
printf( "Client connected \n" );
if( fork() == 0 )
{
read( client_sockfd, &ch, 1 );
printf( "Client send = %c\n", ch );
ch++;
sleep( 5 );
printf( "Server send = %c\n", ch );
write( client_sockfd, &ch, 1 );
close( client_sockfd );
exit (0 );
}
else
close( client_sockfd );
}
}
client in java:
import java.io.*;
import java.net.*;
class clientTCP
{
public static void main(String argv[]) throws Exception
{
String sentence;
String modifiedSentence;
Socket socket = new Socket("127.0.0.1", 10000);
InetAddress adresa = socket.getInetAddress(); //address
System.out.print("Connecting on : "+adresa.getHostAddress()+" with hostname : "+adresa.getHostName()+"\n" );
ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream());
oos.writeObject("HalloXXXX");
ObjectInputStream ois = new ObjectInputStream(socket.getInputStream());
String message = (String) ois.readObject();
System.out.println("Message Received: " + message);
ois.close();
oos.close();
socket.close();
}
}
You can only use ObjectOutputStream with other Java programs which also use ObjectInputStream to read it. It is hard to imagine a C program supporting Java's Serialization format.
If you want to write text I suggest you use PrintWriter/BufferedReader or a better solution is to use BufferedOutputStream and do your own encoding (so you see all errors.
这篇关于在java中交际用C客户端TCP服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!